With
$products = Brand::with('product')->where('id', '=', '1');
You can ready use category
foreach($products as $product){
echo $product->category->id;
}
Actually I may have jumped the gun in marking this as solved, hopefully I don't need to open a new thread.
Category::with('product')->where('id', '=', $id)->get();
produces this query:
select * from `products` where `products`.`category_id` in ('1')
and
Brand::with('product')->where('id', '=', $id)->get();
produces:
select * from `products` where `products`.`brand_id` in ('1')
I want to have access to all Brands where the category_id is 1 so I am using the first (Category::with('product')->where('id', '=', $id)->get();)
which returns the following array:
..so I have no access to the Brand description through this. Any ideas?
Thanks!
Do you want to get all brands of products in a category????
Frontend like:
Brand1 | Brand 2 | Brand 3
List Products
Product 1 | Product 2 | Product 3 | Product 4
Yes so basically selecting a specific Category (for instance Paper, Chair, Tables) will give me all brands under each specific category so selecting:
Paper will list Brand1 and Brand6 Chair will list Brand2 and Brand4
and so on depending on the type of product. Hope this makes sense
@paulocr, I think you should change your Product model to use belongsTo relationship, since category_id and brand_id are in the products table. I don't know if it will work this way.
About your question, I think hasManyThrough is the way to go. Maybe this will work:
class Category extends Eloquent {
public function brands()
{
return $this->hasManyThrough('Brand', 'Product');
}
}
Thanks for all your help. Your help + eager loading + a pivot table + a lot of frustration seem to have given me what I needed.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community