mardi 28 février 2017

Querying Laravel Eloquent Relationship

I have three tables.

  • Categories
  • Products
  • Brands

I have a relation on my categories table to the products like so:

public function products()
{
    return $this->belongsToMany('App\Product','product_sub_categories','subcategory_id','product_id');
}

I have a relation on my Products table to the brands like so:

public function manuf()
    {
        return $this->belongsTo('App\Brand','brand');
    }

I'm querying the categories table to return products of that category by a certain brand.

For example.

I wan to see all products in Cars category with the brand Fiat.

I've tried the following but I feel Im missing something..

 $search = 'fiat';
 $products = $category->products()->where(function($query) use ($search){
                    $query->brand->name = $search;
                })->get();



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2m9sQB4
via IFTTT

Aucun commentaire:

Enregistrer un commentaire