Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 执行高级where子句_Php_Laravel_Eloquent - Fatal编程技术网

Php 执行高级where子句

Php 执行高级where子句,php,laravel,eloquent,Php,Laravel,Eloquent,我将知道如何执行“高级Where”。 我在文档中找不到任何解释我想要的东西。。甚至在那里。 (比照:) 我的查询如下所示: 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` = ?)

我将知道如何执行“高级Where”。 我在文档中找不到任何解释我想要的东西。。甚至在那里。 (比照:)

我的查询如下所示:

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[)]
但我希望我的查询如下所示:

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查询生成器,或者我应该进行手工查询

先谢谢你

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();

事实上,这是您在页面上看到的第一个示例。然而,这个例子是非常不准确的,因为你不会将
的位置与
的位置进行分组,但反过来说..

好的,对不起,我应该尝试一下。我敢肯定,如果是在恋爱关系上,这是行不通的。谢谢你,它的工作原理和我想的一样。为了避免另一篇帖子,你知道我是否可以在带有约束的急切加载上应用first()方法吗?你的意思是对整个查询使用
first
而不是
get
?当然可以。不,只在with方法上。在我的示例中,我只想获取第一个“subtags”,方法返回多维数组。因此,我只会将first()方法应用于这个案例,而不是整个queryNo,它将不起作用,并将导致意外的结果。读这个