Level 1
i think you can use subqueries
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have four tables. And I need to run almost identical queries. Like this:
$model1 = Model1::selectRaw('table1.*, MAX(max_rating) as maxAverageRating')
->orderBy('max_rating', 'desc')
->groupBy('id')
->first();
$model2 = Model2::selectRaw('table2.*, MAX(max_rating) as maxAverageRating')
->orderBy('max_rating', 'desc')
->groupBy('id')
->first();
$model3 = Model3::selectRaw('table3.*, MAX(max_rating) as maxAverageRating')
->orderBy('max_rating', 'desc')
->groupBy('id')
->first();
$model4 = Model4::selectRaw('table4.*, MAX(max_rating) as maxAverageRating')
->orderBy('max_rating', 'desc')
->groupBy('id')
->first();
Is there any way to run this in one query WITHOUT joining?
Please or to participate in this conversation.