繁体   English   中英

Laravel雄辩的按关系分组

[英]Laravel Eloquent group by relation

我正在努力了解如何将Eloquent / Query Builder与关系一起使用。

我有以下几点:

$plantLots = PlantLot::with('controlNumber')
   ->whereHas('controlNumber', function ($q) use ($fromDate, $toDate) {
        $q->where('created_at', '>=', $fromDate);
        if($toDate != '') {
           $q->where('created_at', '=<', $toDate);
        }
        $q->groupBy('creator_id');
   })->get();

我想按creator_id ,但我仍然只获得一个数据集合。

我究竟做错了什么?

更改为

$plantLots = PlantLot::with(['controlNumber' => function($q){
       $q->groupBy('creator_id');
   }])
   ->whereHas('controlNumber', function ($q) use ($fromDate, $toDate) {
     $q->where('created_at', '>=', $fromDate)
       ->when($toDate != '',function($q){
         $q->where('created_at', '=<', $toDate);
      });
  })->get();

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM