Support the ongoing development of →
Database Eloquent
Last updated 3 months ago.


$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')


Brand::with('product')->where('id', '=', $id)->get();


	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: I have no access to the Brand description through this. Any ideas?


Last updated 3 months ago.

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
Last updated 7 years ago.

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');


thank you, I will test it out


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!


Your banner here too?

paulocr paulocr paulocr Joined 6 Oct 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 - All rights reserved.