I want to return all items with have all categories.
$items = DB::table('items')
->join('catitem_item', 'catitem_item.item_id', '=', 'items.id')
->whereIn('catitem_item.catitem_id', $cats)->paginate(10);
This query what I have now return items that have category 1 or 2 or 3. What I need is return items that have category 1 and 2 and 3.
How can I achieve this?
Try this one:
$items = DB::table('items')
->join('catitem_item', 'catitem_item.item_id', '=', 'items.id')
->whereIn('catitem_item.catitem_id', $cats)
->groupBy('items.id')
->having(DB::raw('count(*)'), '=', count($cats))
->select('items.*')
->paginate(10);
With HAVING count(*) = 3
the query will only return items that have all listed categories.
Try like this: Where $cats= [1,2,3];
$items = DB::table('items')
->join('catitem_item', 'catitem_item.item_id', '=', 'items.id')
->whereIn('catitem_item.catitem_id', $cats)
->paginate(10);
its return items for category 1 and 2 and 3.that means its use for AND opration.
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.