Eloquent Order by HasMany Relationship: Three Ways
Imagine you want to load the Model with its related many models, but sort those related results by some column in that related DB table. How to do that?
We found 4 results for "order by relationship".
Imagine you want to load the Model with its related many models, but sort those related results by some column in that related DB table. How to do that?
If you have a query with the `belongsTo` relationship and want to order the records by the column from that related table, the orderBy wouldn't work.
Quite a regular task, but not a simple solution: how to order query results by a belongsTo relation column? I will show you one "bad" way, and two good ways to do it.
Files in the example:
Our team is here to help. Ask us anything about Laravel development and we'll get back to you with personalized guidance.