我知道如何执行'高级所在'。 我没有在文档中找到任何解释我想要的东西..即使在那里。 (cf:http://laravel.com/docs/4.2/queries#advanced-wheres)。
Post::whereHas('international_post_en', function($q) {
$q->where('is_published', 1);
})->whereHas('categories', function($q) {
$q->where('name', 'test-one');
})->orWhereHas('subcategories', function($q) {
$q->where('name', 'test-two');
})->with('categories', 'subtags')
->get();
我的查询如下:
select * from `posts` where `posts`.`deleted_at` is null and (select count(*) from `international_posts_en` where `international_posts_en`.`posts_id` = `posts`.`id` and `is_published` = ?) >= 1 and (select count(*) from `categories` inner join `posts_has_categories` on `categories`.`id` = `posts_has_categories`.`categories_id` where `posts_has_categories`.`posts_id` = `posts`.`id` and `name` = ?) >= 1 or (select count(*) from `subcategories` inner join `posts_has_subcategories` on `subcategories`.`id` = `posts_has_subcategories`.`subcategories_id` where `posts_has_subcategories`.`posts_id` = `posts`.`id` and `name_en` = ?) >= 1
但我希望我的查询看起来像这样:
select * from `posts` where `posts`.`deleted_at` is null and (select count(*) from `international_posts_en` where `international_posts_en`.`posts_id` = `posts`.`id` and `is_published` = ?) >= 1 and [(](select count(*) from `categories` inner join `posts_has_categories` on `categories`.`id` = `posts_has_categories`.`categories_id` where `posts_has_categories`.`posts_id` = `posts`.`id` and `name` = ?) >= 1 or (select count(*) from `subcategories` inner join `posts_has_subcategories` on `subcategories`.`id` = `posts_has_subcategories`.`subcategories_id` where `posts_has_subcategories`.`posts_id` = `posts`.`id` and `name_en` = ?) >= 1[)]
(抱歉它不太可读)
您可以在括号内看到更改。 所以,我希望将whereHas和orWhereHas子句分组在括号内。
是否可以使用Laravel Query Builder,还是应该进行手工查询?
提前谢谢。
答案 0 :(得分:1)
Post::whereHas('international_post_en', function($q) {
$q->where('is_published', 1);
})->where(function ($q) {
$q->whereHas('categories', function($q) {
$q->where('name', 'test-one');
})->orWhereHas('subcategories', function($q) {
$q->where('name', 'test-two');
});
})->with('categories', 'subtags')->get();
实际上,这是您linked页面上的第一个示例。然而,这个例子非常不准确,因为你不会将and
与or
组合在一起,而是反过来..