本文介紹了雄辯的 laravel WhereIn All的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想返回所有類別的所有項目.
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);
這個查詢我現(xiàn)在擁有的返回類別為 1 或 2 或 3 的項目.我需要的是返回類別 1、2 和 3 的項目.
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?
推薦答案
試試這個:
$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);
使用 HAVING count(*) = 3
查詢將只返回具有所有列出類別的項目.
With HAVING count(*) = 3
the query will only return items that have all listed categories.
這篇關于雄辯的 laravel WhereIn All的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!