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?

  • Rights and more will ask the correct question. Now it is absolutely not clear what you need and what is not working out. - ArchDemon
  • @ArchDemon changed the question, I hope now explained more clearly) - Alex_01
  • 2
    On pure SQL, there will be something like select * from brands where id in(select brand_id from products where category_id=нужная_категория) - Mike
  • one
    @Mike you correctly answered. I would just add SELECT 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. - ArchDemon
  • @Mike thanks. Your request code helped. Only I had to make such a query select * from hp_brands` where exists (select * from hp_products where hp_products . brand_id = hp_brands . id and category_id = '23') `for you, probably, you will not be able to do laravel. - Alex_01

0