Eloquent eager load Order by
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Mastering Eloquent Eager Loading with Order By in Laravel
Body:
Eloquent eager loading has become an essential tool for developers to build efficient and scalable applications using Laravel. In this article, we will focus on the problem of ordering eagerly loaded relationships by a specific column. Let's assume you have a one-to-one relationship between students and exams where each student can only take one exam and vice versa. Here is your current code for retrieving students with their respective exams in descending order based on exam results:
Student::with('exam')->orderBy('exam.result', 'DESC')->get()
However, it seems to be failing or not ordering as expected. So let's dig deeper into the issue and provide a comprehensive solution.
1. The Eloquent eager loading process: When you perform an eager load with Laravel, it retrieves all related models at once instead of making multiple queries for each model. This saves time and improves performance when dealing with a lot of data.
2. Understanding the query: In this case, your code is trying to get students with their exams while ordering by 'exam.result' in descending order. The code should ideally retrieve students along with sorted results based on the descending order of their exam results.
3. Possible causes and solutions: One possible cause for your issue might be that you are trying to sort a nested relation. In this case, you need to specify the exact level of nested relations you want to order. Let's break it down into steps:
- If you have one-to-many relationships between students and exams, your code should be working correctly. However, ensure that you are ordering the students based on the relationships with their respective exams.
- If you have a tree structure of exams or multiple levels of relationships, you might face issues. In this case, you'll need to specify which level you want to order: the students themselves, their exam results, or any other nested relations.
4. Example code for different scenarios: Here are some examples of using eager loading with ordering based on various nested relationship levels:
- Order students by exam results:
Student::with([
'exam' => function ($query) {
$query->orderBy('result', 'DESC');
}
])->get()
- Order students by the number of their completed exams:
Student::withCount('exams')->orderBy('exam_count', 'DESC')->get()
5. Conclusion: Eager loading in Laravel is a powerful tool to optimize query performance, but it can be tricky when dealing with nested relationships and ordering. By understanding the underlying structure of your data model and carefully specifying the relations you want to load and sort, you can successfully implement eager loading with order by. If needed, always test different approaches and consult documentation or seek help from the Laravel community on sites such as https://laravelcompany.com/blog for advice and support. Happy coding!