2

Going straight to the point here. I have a search functionality on my List of Items. However, I also want to search all items by category.

Here's what I have done so far. I am really new to laravel and eloquent so please bear with me.

Category Table

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('code', 30);
        $table->string('name');
        $table->softDeletes();
        $table->timestamps();
    });
}

Item Table

public function up()
{
    Schema::create('items', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('category_id')->unsigned();
        $table->string('code', 30);
        $table->string('name');
        $table->softDeletes();
        $table->timestamps();
    });

    Schema::table('items', function($table){
        $table->foreign('category_id')->references('id')->on('categories');
    });
}

Here's my code:

public function search(){
    $q = Input::get ( 'q' );
    if($q != ""){
        $items = Item::with('Category')
                    ->where ( 'name', 'LIKE', '%' . $q . '%' ) 
                    ->orWhere ( 'code', 'LIKE', '%' . $q . '%' )
                    ->whereHas('Category', function($cat){
                        $cat->where('name', 'hardware');
                    })->paginate(1)->setPath('');

        $items = $items->appends ( array (
                    'q' => Input::get ( 'q' ) 
            ) );
    }else{
        $items = Item::orderBy('id', 'desc')->paginate(10);
    }

    $softDeletesItems = Item::onlyTrashed()->get();
    return view('item.index', compact('items', 'softDeletesItems'));
}

is there a way for me to do a orWHere('category.name', 'LIKE', '%'. $q .'%')?

14
  • can you proviide both item and category table columns so we can check Commented Sep 28, 2017 at 3:37
  • hi @iCoders give me a second I will post it. Commented Sep 28, 2017 at 3:38
  • @iCoders can u check again? Commented Sep 28, 2017 at 3:40
  • ->whereHas('Category', function($query) use($q){ $query->where('name', 'hardware')->orWHere('name', 'LIKE', '%'. $q .'%') Commented Sep 28, 2017 at 3:46
  • @iCoders yes sir is there a way for me to do a ->orWhere ( 'categories.name', 'LIKE', '%' . $q . '%' )? Commented Sep 28, 2017 at 3:47

2 Answers 2

2
->whereHas('Category', function($query) use($q){

                       $query->where('name', 'LIKE', '%'. $q .'%')

         })->paginate(1)->setPath('');
Sign up to request clarification or add additional context in comments.

Comments

2

Here's what I did to achieve it.

    $items = Item
            ::join('categories', 'items.category_id', '=', 'categories.id')
            ->where ( 'items.name', 'LIKE', '%' . $q . '%' ) 
            ->orWhere ( 'items.code', 'LIKE', '%' . $q . '%' )
            ->orWhere('categories.name', 'LIKE', '%' . $q . '%')
            ->select('*')
            ->paginate(1)->setPath('');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.