Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

faisals10's avatar

Convert SQL query to eloquent query with relationship in Laravel

I want to convert this SQL query to an eloquent query :

SELECT round(AVG(rating.reviews),2) as avg_rating, COUNT(excursions.id) as review_total, excursions.name FROM reviews LEFT JOIN excursions ON reviews.excursion_id = excursions.id GROUP BY excursion_id ​

0 likes
1 reply
jlrdw's avatar

You want to rework the query to use relations, and not use a GROUP BY? You would need to setup the relations, run migrations, etc. Or use query builder, the ORM also has query builder built in.

Please or to participate in this conversation.