[英]Show the posts which have specific tags or categories
我想顯示標題中提到的具有特定標簽或類別的帖子; 例如,我的一個帖子有 3 個標簽“php”、“laravel”、“regex”,我想獲得那些在多對多關系中帶有 laravel 標簽的帖子。
我更喜歡 leftJoin 而不是 whereHas 因為 whereHas 很慢。
試試這個查詢,看看區別:
$posts = Post::leftJoin('post_tags', 'post_tags.post_id', '=', 'posts.id')
->leftJoin('post_routes', 'post_routes.post_id', '=', 'posts.id')
->where('name', $tagName)
->where('route', $routeName)
->get();
首先,確保您在模型上設置了正確的關系。 然后使用查詢關系存在。 這樣你就可以:
$posts = Post::whereHas('tags', function ($tags) use ($tagName) {
$tags->where('name', $tagName);
})
->whereHas('ORMRoute', functions ($routes) use ($routeName) {
$routes->where('route', $routeName);
})
->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.