PaulDiamant's avatar

Distant relationship, select brands that products use

I'm working on an ecommerce app, and I have the following tables:

products:
id, name, description, category_id, brand_id, sku, created_at... etc
brands:
id, name
categories:
id,name,parent_id

Say I have this, to retrieve products with those categories, how will I also retrieve all brands that products use without having duplicates? Since different brands can have the same brand, so it won't be displayed in the filtering sidebar.

$products = Product::whereIn('category_id', [1, 2, 3])->get();
0 likes
2 replies
shez1983's avatar

r u wanting to display brands in side bar?

if so then you need to dobrand::wherehas(prod.catrgories, func() {...})->with()

Please or to participate in this conversation.