Answer the question
In order to leave comments, you need to log in
How to sort a table by the value of an aggregate function?
There are 2 tables:
restaurant: - id, name, ...
reviews: id, restaurant_id, rating.
How can I implement a query to sort restaurants by average rating?
Answer the question
In order to leave comments, you need to log in
$restaurants = DB::table('restaurants')
->join('restaurant_reviews', function($join)
{
$join->on('restaurants.id', '=', 'restaurant_reviews.id_restaurant')
->orderByRaw('avg(rating)');
})
->get();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question