Hello. I have such a structure.
categories | products | brands name name name slug slug slug category_id brand_id I want to make a filter by manufacturer (the usual filter as in any online store). But, since manufacturers do not belong to categories, I have to do this: I take an array of goods that are in the category (for example) refrigerators, and then I do it
/** * Получаем производителей * * @param array $ids * @return mixed */ public function brands($ids = []) { return Brand::whereHas('products', function($q) use($ids) { $q->whereIn('id', $ids); } )->get(); } It all works until I go through any filter. The question is actually how to bring the producers to the category differently? In English, I was advised to categorize manufacturers. But there is another problem. Every time you have to choose which category the manufacturer belongs to. Is there any universal solution?
select * from brands where id in(select brand_id from products where category_id=нужная_категория)- MikeSELECT DISTINCT brand_id. The problem is that such a query will be executed slowly with a large base. It is possible, as you have already been told, to make an intermediate table, where to list all categories of the manufacturer. - ArchDemonselect * fromhp_brands` where exists (select * fromhp_productswherehp_products.brand_id=hp_brands.idandcategory_id= '23') `for you, probably, you will not be able to do laravel. - Alex_01