mysql - Sum and order by desc with query builder (laravel) -
i have som results of multiple table, selected
$places = place::with(['rates' => function($query) { $query->select('id', 'place_id', 'criterion_id', 'value'); }])->select('id', 'coords')->get();
i need order reslut sum (or avg) of values. how can this? tried use
$query->select('id', 'place_id', 'criterion_id', 'value')->sum('value');
but doesn't work.
if works you. can use join
instead of eager loading.
the code this:
place::leftjoin('rates', 'places.id', '=', 'rates.place_id') ->selectraw('places.*, sum(rates.value) sumofratevalues') ->groupby('places.id') ->orderby('sumofratevalues', 'desc') ->get();
Comments
Post a Comment