2017-04-20 73 views
0

我很努力去理解如何在关系中使用Eloquent/Query Builder。Laravel通过关系的雄辩小组

我有以下几点:

$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,但我仍然只得到数据的单个集合。

我在做什么错?

回答

0

将其更改为

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

希望它能帮助。