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.

i have such table

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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -