2017-04-20 13 views
0

Eloquent/Query Builderをリレーションシップで使用する方法を理解するのに苦労しています。Laravel Eloquent group by relation

私は、次のしている:

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

への変更、それがお役に立てば幸いです。