简体   繁体   中英

How do I make a selection from two related tables?

I have a table with products, relation to the tables of categories and collections as many to many. I need to make a selection by category and collection so that I get the products that are included there

I have written some selection, but it does not take into account that the products are related with collections and categories.

$filter_collections = [1, 3];
$filter_categories = [2, 5];


$products = Product::whereHas('collection', function ($query) use ($filter_collections) {
                $query->whereIn('collection_id', $filter_collections);
            })->whereHas('category', function ($query) use ($filter_categories) {
                $query->whereIn('category_id', $filter_categories);
            })->get();

How do I make the selected products belong to the specified categories and collections?

Untested, but can you please try this:

$products = Product::
  whereIn('collection_id', $filter_collections)
->orWhereIn('category_id', $filter_categories)
->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM