Laravel 4: how to "order by" using Eloquent ORM
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Laravel 4: Efficiently Ordering Results with Eloquent ORM
Introduction:
Ordering is a crucial aspect of any application that deals with data. In Laravel, you can efficiently sort your results using its Object Relational Mapper (ORM) named Eloquent. This blog post will answer how to order by 'id' descending in Laravel 4 while providing relevant code examples and best practices.
1. Understanding the Basics:
First, let us understand how data retrieval works with Eloquent ORM. Let's assume that you have a simple Post model defined as follows:
class Post extends Model {
protected $table = 'posts';
// Other attributes, methods...
}
To obtain all the posts in your application, you use the "all()" method as we have done above.
$posts = $this->post->all();
However, to get the desired results when ordering by 'id' descending, we need to add an orderBy() query. The syntax for adding this method is: "orderBy('column', 'direction')".
2. Adding Order By Functionality:
You can make use of Eloquent ORM's orderBy() method to sort your results in the desired manner. Let's update our previous code with the new orderBy() functionality:
$posts = $this->post->all()->orderBy('id', 'DESC');
Here, we are using the "DESC" keyword which stands for descending. It ensures that the query sorts in a reverse order (i.e., from highest to lowest).
3. Understanding the Order By Method Syntax:
The orderBy() method supports various query builder clauses. You can use it with any column name, whether it's defined explicitly in your model or not. It also takes two arguments - the column name and the direction. The direction can be either 'ASC' for ascending order (lowest to highest) or 'DESC' for descending order (highest to lowest).
4. Using Multiple Order By Clauses:
If you want to use multiple clauses in your query, you need to call the orderBy() method as many times as needed. To do this, simply chain the method calls with a comma separating them:
$posts = $this->post->all()->orderBy('title', 'ASC')->orderBy('date', 'DESC');
In this example, the first orderBy() sorts by the 'title' column in ascending order (alphabetical order), while the second one sorts by the 'date' column in descending order (newest to oldest).
5. Optimizing Performance and Reducing Database Queries:
When working with large datasets, it is essential to optimize your code for performance and efficiency. To limit database queries, you can use the "with()" method. It allows you to run multiple Eloquent relationships in a single query instead of running them one by one. Here's an example:
$posts = $this->post->all()->orderBy('id', 'DESC');
$comments = $this->comment->all();
DB::transaction(function() use ($posts, $comments) {
$this->post->attachComments($posts->pluck('id'), $comments);
});
In this example, we first load the posts and attach related comments. By using a transaction block, we ensure that all queries are run in a single database transaction if no errors occur. This approach can reduce the number of database queries needed for efficient data retrieval while maintaining data integrity.
6. Conclusion:
Ordering results with Eloquent ORM in Laravel 4 is simple and straightforward. By understanding the "all()", "orderBy()" and other methods, you can effectively manage your application's data. This blog post discussed how to sort by 'id' descending, using examples that cover single and multiple order clauses, and optimizing performance. Remember to explore further Eloquent documentation for advanced features and techniques to improve your data retrieval skills.
7. Relevant Backlinks:
For more information on Eloquent ORM in Laravel 4, check out the following resources from https://laravelcompany.com/blog/:
- [Laravel 5.x Series Part II - Introduction to Eloquent Relationships](https://laravelcompany.com/blog/laravel-5-introduction-to-eloquent-relationships)
- [Eloquent: Model Hierarchies and Relations in Laravel 4](https://laravelcompany.com/blog/model-hierarchies-and-relations-in-laravel-4)
- [Laravel 5.x Series Part I - Eloquent ORM Basics](https://laravelcompany.com/blog/laravel-5-eloquent-orm-basics)