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.

  • 写回答

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..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集