当我们在Laravel 5.7中使用条件子句时,如何用括号括起雄辩的查询

时间:2019-02-14 09:58:23

标签: laravel laravel-5 eloquent

我有一个查询,该查询应仅将所有帖子及其英文翻译载入。
如果用户输入关键字,则仅返回带有该关键字标题的英文帖子。

if ($searchKeywords||$searchCategory){
    $posts = Post::
        select('post_translations.post_id AS id', 'post_translations.title AS title', 'category_id', 'locale')
           ->join('post_translations', 'posts.id', '=', 'post_translations.post_id')
           ->where(‘post_translations.locale','=','en')
           ->when($searchKeywords, function ($query, $searchKeywords) {
                 return $query->where('post_translations.title', $searchKeywords)->orWhere('post_translations.title', 'like', '%' . $searchKeywords . '%');
            })
           ->when($searchCategory, function ($query, $searchCategory) {
                  return $query->where('category_id', '=', $searchCategory);
            ->paginate(20);
        }
else
    $posts = Post::select('id', 'title', 'category_id')->orderBy('title')->paginate(20);

生成的查询是这样的:

SELECT `post_translations`.`post_id` AS `id`, `post_translations`.`title` AS `title`, `category_id` 
FROM `posts` inner join `post_translations` 
ON `posts`.`id` = `post_translations`.`post_id` 
WHERE `post_translations`.`locale` = 'en' 
AND `post_translations`.`title` = 'About' 
OR `post_translations`.`title` like 'About’  
LIMIT 20 OFFSET 0

这使我获得了关于About的所有3个帖子的翻译。
这是因为 orWhere

如何更改雄辩的查询以生成这样的查询?

SELECT `post_translations`.`post_id` AS `id`, `post_translations`.`title` AS `title`, `category_id` 
FROM `posts` inner join `post_translations` 
ON `posts`.`id` = `post_translations`.`post_id` 
WHERE `post_translations`.`locale` = 'en' 
AND (`post_translations`.`title` = ‘About' OR `post_translations`.`title` like 'About’  )
LIMIT 20 OFFSET 0

问题不是此问题的重复,因为我还有一个子查询级别。
How do you wrap Laravel Eloquent ORM query scopes in parentheses when chaining?

2 个答案:

答案 0 :(得分:2)

在条件查询中添加两个条件,如下所示:

if ($searchKeywords) {
    $posts = Post::select('post_translations.post_id AS id', 'post_translations.title AS title', 'category_id', 'locale')
       ->join('post_translations', 'posts.id', '=', 'post_translations.post_id')
       ->where(‘post_translations.locale','=','en')
       ->where(function ($query) use ($searchKeywords) {
             $query->where('post_translations.title', $searchKeywords)
                   ->orWhere('post_translations.title', 'like', '%' . $searchKeywords . '%');
        })
        ->paginate(20);
}

答案 1 :(得分:0)

我已经解决了以下代码:

if ($searchKeywords||$searchCategory){
    $posts = Post::
            select('post_translations.post_id AS id', 'post_translations.title AS title', 'category_id', 'locale')
            ->join('post_translations', 'posts.id', '=', 'post_translations.post_id')
            ->when($searchKeywords, function ($query, $searchKeywords) {
                return $query->where('post_translations.locale','=','en')
                             ->where(function ($query) use ($searchKeywords) { 
                                  $query->where('post_translations.title', $searchKeywords)->orWhere('post_translations.title', 'like', '%' . $searchKeywords . '%');
                              });

            })
            ->when($searchCategory, function ($query, $searchCategory) {
               return $query->where('post_translations.locale','=','en')
                            ->where(function ($query) use ($searchCategory) { 
                                $query->where('category_id', '=', $searchCategory);
                             });
            })
            ->paginate(20);
}
else
    $posts = Post::select('id', 'title', 'category_id')->orderBy('title')->paginate(20);