Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Search Relationship

I have two models which are related. I am trying to do a search in Products and only display the actual search results instead of ALL products of the category in which the product was found. I DO NOT want to search for any categories, since the categories will ALWAYS be displayed no matter what was searched for and no matter what was found.

Example. I have the following categories:

- Food
- Drinks
- Candy

My "Food" category has the following products:

- Strawberry
- Apple
- Banana

My "Drinks" category has the following products:

- Banana Cocktail
- Beer
- Cola

My "Candy" category has the following products:

- Strawberry Lollipop
- Chocolate Bar
- Banana Ice Cream

So, what I WANT to achieve is the following. I do a search for a product called "Banana". What I WANT to be displayed is:

Category Food
- Product Banana

Category Drinks
- Product Banana Cocktail

Category Candy
- Product Banana Ice Cream

But my issue is, with my code, if I perform a search for "Banana", it displays the category in which banana is found, and it returns and displays ALL products in that category instead of ONLY the products that I searched for. How can I achieve it so only the products that was searched for are displayed?

Categories Model:

class Categories extends Eloquent {

    public function products()
    {
        return $this->hasMany('Products');
    } 
}

Products Model:

class Products extends Eloquent {

    public function categories()
    {
        return $this->belongsTo('Categories');
    }
}

My Controller:

    $searchString       = Input::get('search');

    if($searchString)
    {
        $categories = Categories::with('products')->orderBy($order, $by)->whereHas('products', function ($query) use ($searchString){
            $query->where('name', 'like', '%'.$searchString.'%');
        })->get();
    }
    else {
        $categories     = Categories::with('products')->orderBy($order, $by)->get();
    }

My View:

@foreach($categories as $category)
    {{ $category->name }} // Show the category name

    @foreach($category->products as $product)
    {{ $product->name }} // Show all products in that category

    @endforeach
@endforeach
like image 712
Hardist Avatar asked Aug 07 '15 07:08

Hardist


1 Answers

I don't know what how are you viewing the results, but I think if you just eager load the products on the categories you should be good.

$categories = Categories::whereHas('products', function ($query) use ($searchString){
        $query->where('name', 'like', '%'.$searchString.'%');
    })
    ->with(['products' => function($query) use ($searchString){
        $query->where('name', 'like', '%'.$searchString.'%');
    }])->get();

foreach($categories as $category){
    echo $category->name . ':' . PHP_EOL;
    foreach($category->products as $product){
        echo . '-' . $product->name . PHP_EOL;
    }
}
like image 96
Pawel Bieszczad Avatar answered Nov 06 '22 10:11

Pawel Bieszczad