在Laravel 5.7中使用条件子句时,如何用括号包装雄辩的查询
我有一个查询,应该加载所有的职位,只有他们的英文翻译。 如果用户输入一个关键字,它只返回包含该关键字的标题的英文文章在Laravel 5.7中使用条件子句时,如何用括号包装雄辩的查询,laravel,laravel-5,eloquent,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(
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
那就把这篇文章的3个翻译都还给我吧。
这是因为在哪里
如何更改雄辩的查询以生成这样的查询
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
这个问题不是这个问题的重复,因为我还有一级子查询。
在where查询中添加这两个条件,如下所示:
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);
}
在where查询中添加这两个条件,如下所示:
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);
}
我已经用这个代码解决了:
我已经用这个代码解决了:
这应该行得通,反正我的查询有点复杂,我已经更新了。我有两个搜索字段,$searchKeywords和$searchCategory。所以我需要保留when条件。你可以在when中以同样的方式添加where条件。我不明白。如果我喜欢这个,问题也是一样的$查询->where'post_translations.locale'、'='、'en'->where'post_translations.title'、$searchKeywords->或where'post_translations.title'、'like'、'%'$搜索关键词“%”;因为它生成:WHERE post_translations.locale='en'和post_translations.title='About'或post_translations.title类似'About'的限制20偏移量0以这种方式使用$query->WHERE'post_translations.locale'、'='、'en'->WHERE函数$query使用$searchKeywords{$query->where'post_translations.title',$searchKeywords->or where'post_translations.title','like','%'.$searchKeywords.'%';}这应该行得通,反正我的查询有点复杂,我已经更新了。我有两个搜索字段,$searchKeywords和$searchCategory。所以我需要保留when条件。你可以在when中以同样的方式添加这个where条件。我不明白。如果我这样做,问题是一样的。$query->where'post_translations.locale','=','en'->where'post_translations.title'、$searchKeywords->OR where'post_translations.title'、'like'、'%'.$searchKeywords.'%';因为它生成:where'post_translations.locale='en'和post_translations.title='About'或post_translations.title'like'About'限制20偏移量0以这种方式使用$query->->'where'post_translations.locale'、'en'='此处函数$query使用$searchKeywords{$query->where'post_translations.title',$searchKeywords->or where'post_translations.title','like','%'.$searchKeywords.%';}