duan47676379
2014-11-12 19:53
浏览 41
已采纳

执行高级where子句Eloquent

I would know how to perform an 'Advanced Where'. I didn't find anything in the documentation that explain what I want.. even there. (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();

My query look like this:

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

But I want my query to look like this:

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[)]

(Sorry It is not very readable)

You can see the changes within the brackets. So, I want the whereHas and the orWhereHas clause to be grouped within parenthesis.

Is it possible with the Laravel Query Builder or should I make a handmade query?

Thank you in advance.

图片转代码服务由CSDN问答提供 功能建议

我知道如何执行'Advanced Where'。 我在文档中找不到任何解释 我想要什么..即使在那里。 (cf: http://laravel.com/ docs / 4.2 / queries#advanced-wheres )。

  Post :: whereHas('international_post_en',function($ q){
 $ q-&gt  ; where('is_published',1); 
}) - > whereHas('categories',function($ q){
 $ q-> where('name','test-one'); \  n}) - > orWhereHas('subcategories',function($ q){
 $ q-> where('name','test-two'); 
}) - > with('categories'  ,'subtags')
  - > get(); 
   
 
 

我的查询如下所示:

  select * from`posst`其中`posts` .deleted_at`为null,并且从`international_posts_en`中选择count(*),其中`international_posts_en``posts_id` =`posts` .id`和`is_published` =?  )> = 1和(从`categories`内连接`posts_ha中选择count(*)  s_categories`在`categories` .id` =`posts_has_categories``categories_id`其中`posts_has_categories``posts_id` =`posts` .id`和`name` =?)> = 1或(select count(  *)来自`subcategories`内连接`posts_has_subcategories`在`subcategories` .id` =`posts_has_subcategories``subcategories_id`其中`posts_has_subcategories``posts_id` =`posts` .id`和`name_en` =?)  > = 1 
   
 
 

但我希望我的查询看起来像这样:

  select * from` posts  ``posts``deleted_at`为null,并且从`international_posts_en`中选择count(*),其中`international_posts_en``posts_id` =`posts` .id`和`is_published` =?)> = 1  [(](从`categories`内连接`posts_has_categories`中选择count(*)来'于`categories` .id` =`posts_has_categories``categories_id`其中`posts_has_categories``posts_id` =`posts` .id` 和'name` =?)> = 1或(从`子类别`选择计数(*)``子类别`的内部连接`posts_has_subcategories```````poss_has_subc  ategories``subcategories_id`其中`posts_has_subcategories``posts_id` =`posts` .id`和`name_en` =?)> = 1 []] 
   
 
  

(抱歉它不太可读)

您可以在括号内看到更改。 因此,我希望将whereHas和orWhereHas子句分组在括号内。

是否可以使用Laravel查询生成器或者我应该进行手工查询?

提前谢谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumalu9257 2014-11-12 20:42
    已采纳
    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();
    

    In fact this is the very first example on the page you linked. However that example is pretty inaccurate, since you wouldn't group and wheres against or where, but the other way around..

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题