Laravel 4: how to "order by" using Eloquent ORM

Laravel 4: how to "order by" using Eloquent ORM

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Chaining Order By in Laravel 4 Eloquent Queries

In Laravel 4, the Eloquent ORM provides a fluent interface for constructing database queries, but understanding how to chain methods like orderBy requires looking past the surface syntax. When you define a model relation or access a table directly via $this->post, you are holding an instance of the Eloquent Builder. This builder accumulates query constraints and sorting logic into an underlying SQL statement that executes only when you retrieve results.

Consider the controller snippet from your question:

$posts = $this->post->all();

This line retrieves all records from the posts table in their default order, which is typically the primary key ascending. To change this behavior, you must chain the orderBy method onto the builder instance before calling all(). The syntax provided in your question:

->orderBy('id', 'DESC');

is the correct approach for Laravel 4, but it must be inserted into the query chain immediately. Here is how the complete statement should look:

$posts = $this->post->orderBy('id', 'DESC')->all();

The critical detail here is method chaining. Eloquent builds the SQL string dynamically as you add methods. If you assign $this->post to a variable first, you must ensure you are calling orderBy on that specific instance before invoking all().

Why Chaining Matters

If you separate these calls incorrectly, such as:

$posts = $this->post;
$posts->all(); // Missing the orderBy call here

the sorting instruction is lost. Eloquent does not maintain state across unrelated variables unless you explicitly pass the builder instance around. The orderBy method modifies the internal query array, which eventually compiles into a SQL ORDER BY clause. In Laravel 4, this specific syntax ensures the database engine sorts the results before returning them to your application.

Alternative Syntax and Column Handling

While specifying 'id' works perfectly fine, Eloquent also allows you to reference the model's primary key without naming it explicitly. You can use orderBy('created_at', 'desc') to sort by timestamps or orderBy('title') for alphabetical sorting. The direction of the sort is flexible; you can use 'asc', 'ASC', 'DESC', or even a boolean true for ascending and false for descending, though explicit strings are clearer for intent.

Performance Considerations

Sorting at the database level using Eloquent's orderBy is almost always preferred over sorting in PHP after retrieval. Fetching all records into memory and then calling usort() or similar functions on a large dataset creates unnecessary overhead. By pushing the ORDER BY clause to the SQL query, you leverage the database engine's internal indexes. If your id column is indexed (which it usually is as a primary key), the sort operation becomes negligible in cost compared to fetching the data itself.

Common Pitfalls

A frequent mistake when transitioning from raw queries or other frameworks is attempting to mix eager loading with ordering incorrectly. In Laravel 4, if you are eager loading relationships, the ordering applies to the parent table unless you use orderBy within the relationship definition itself. For example:

$posts = $this->post->with('comments')->orderBy('id', 'DESC')->get();

Here, the sort order affects the main collection of posts. If you need to sort the comments relative to their parent post, you would need a different approach involving orderBy within the relationship scope. Always verify that the column you are sorting against actually exists in the target table to avoid runtime errors during query compilation.

Our network