Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

Once you group it collapses the rows into groups so I'm guessing that you're seeing an order that you don't expect. Let's say you have 3 rows all with the same user_id but different model_id. The database will aggregate those 3 rows into 1 row and pick the first model_id as the result for that column. When you order by model_id, the order is pretty random relative to their relationship to user_id. I hope that makes sense.

0

Yes you're exactly right. The order of the results is coming back in the reverse of what I want. Reading around on stackoverflow I'm seeing that it can be a challenge to use groupby and orderby on the same query in mysql. The solutions recommended are to use a subquery first to create a temporary table, orderby on those results, then group by on the 'main' query as the last step. not sure this is possible in eloquent?

0

Here is the SQL I'm trying to convert into eloquent or query builder

http://laravel.io/bin/vBk6V

Any ideas on how to turn that query into eloquent? I'm 95% of the way there, but can't figure out how to implement that first select into a subquery, which is the only way I can get ORDER BY to work before a GROUP BY

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.