N
N
Nikita Dergachov2017-07-28 17:05:59
Laravel
Nikita Dergachov, 2017-07-28 17:05:59

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

1 answer(s)
N
Nikita Dergachov, 2017-07-28
@vanillathunder

$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 question

Ask a Question

731 491 924 answers to any question