How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Use orderBy in Laravel Eloquent (with Examples)
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > How to Use orderBy in Laravel Eloquent (with Examples)
Web Development

How to Use orderBy in Laravel Eloquent (with Examples)

how7o
By how7o
Last updated: April 20, 2026
6 Min Read
Laravel Eloquent orderBy — code snippet sorting posts by id descending with arrow icons
SHARE

Sorting query results is one of the most common things you do with Eloquent, and the laravel eloquent order by clause gives you several ways to do it — from the basic orderBy() builder to shortcuts like latest() and custom local scopes. This guide walks through every ordering pattern I use in production Laravel apps, starting with the simplest “sort posts by newest ID first” case and ending with multi-column sorts and reusable model scopes.

Contents
  • TL;DR
  • Basic orderBy usage
    • The latest() and oldest() shortcuts
  • Sorting by multiple columns
  • Reusable ordering with a local scope
  • Random order
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-21 on Laravel 11 with PHP 8.3. Originally published 2022-11-06, rewritten and updated 2026-04-21.

TL;DR

Call Post::orderBy('id', 'DESC')->get() to sort by ID descending, or use the shorthand Post::orderByDesc('id')->get(). For created-at sorts use Post::latest()->get(). Chain multiple orderBy() calls for multi-column sorts. For a reusable ordering, define a local scope like scopeIdDescending() on the model and call it as Post::idDescending()->get().

Basic orderBy usage

The orderBy() method on any Eloquent query takes a column name and a direction (asc or desc, case-insensitive). To pull every post newest-first by primary key:

$posts = Post::orderBy('id', 'DESC')->get();
// or the shorthand:
$posts = Post::orderByDesc('id')->get();

Both produce the same SQL: SELECT * FROM posts ORDER BY id DESC. The default direction is ascending, so Post::orderBy('id')->get() is equivalent to Post::orderBy('id', 'asc')->get().

The latest() and oldest() shortcuts

For the extremely common “most recent first” case Eloquent ships a shortcut that defaults to the created_at column:

$posts = Post::latest()->get();           // ORDER BY created_at DESC
$posts = Post::latest('published_at')->get(); // different column
$posts = Post::oldest()->get();           // ORDER BY created_at ASC

These are purely syntactic sugar for orderBy('created_at', 'desc'/'asc'), but they read nicely in controllers and blade components.

Sorting by multiple columns

Chain multiple orderBy() calls and Eloquent stacks them in the ORDER BY clause in call order. This is how you implement a “sort by status, then newest first within status” list:

$posts = Post::orderBy('status', 'asc')
    ->orderByDesc('created_at')
    ->get();
// SELECT * FROM posts ORDER BY status ASC, created_at DESC
laravel eloquent order by — orderBy, orderByDesc, latest, multi-column, inRandomOrder flow

Reusable ordering with a local scope

If the same ordering shows up across many queries, promote it to a local scope on the model. That’s what idDescending() in the original question is — a custom scope, not a built-in method. Define it on the model like this:

// app/Models/Post.php
class Post extends Model
{
    public function scopeIdDescending($query)
    {
        return $query->orderBy('id', 'DESC');
    }
}

Laravel strips the scope prefix and lowercases the first letter, so you call it as:

$posts = Post::idDescending()->get();
// or combined with other builders:
$posts = Post::idDescending()->where('status', 'published')->paginate(20);

Scopes keep controllers readable and mean you can change the canonical ordering in one place. For one-off orderings, stick with plain orderBy().

Random order

For “pull five random posts” type features, inRandomOrder() is the built-in:

$featured = Post::inRandomOrder()->limit(5)->get();

It emits ORDER BY RAND() (MySQL) or the driver-equivalent. Fine for small tables; for large datasets it’s cheaper to pick IDs in PHP (array_rand against Post::pluck('id')) and run a whereIn.

Frequently asked questions

What’s the difference between orderBy('id', 'DESC') and orderByDesc('id')?

They produce identical SQL. orderByDesc('id') is a shorthand that drops the second argument — pick whichever reads cleaner in your code. There’s a matching orderBy('id', 'asc') / no shorthand for ascending since ascending is the default.

How do I sort by multiple columns in Eloquent?

Chain orderBy() calls — the first one is the primary sort, the next is the tiebreaker, and so on. For example: Post::orderBy('status')->orderByDesc('created_at')->get() sorts by status ascending, then newest first within each status.

Is latest() the same as orderBy('created_at', 'DESC')?

Yes by default — latest() sorts by created_at descending. You can pass a different column: Post::latest('published_at')->get(). The mirror shortcut is oldest() which sorts ascending.

What does idDescending() do — is it built into Laravel?

No, it’s not built in. In the original snippet it’s a custom local scope defined on the model (public function scopeIdDescending($query) { return $query->orderBy('id', 'DESC'); }). Once defined, you call it as Post::idDescending()->get(). Scopes are handy when the same ordering repeats across controllers.

How do I fetch rows in random order?

Use inRandomOrder(): Post::inRandomOrder()->limit(5)->get(). Under the hood it adds ORDER BY RAND() (or the driver’s equivalent), which is fine for small tables but expensive on large ones — for big datasets, prefer picking random IDs in PHP and fetching by ID.

Related guides

  • How to Install Laravel on Ubuntu — set up the framework before running any Eloquent query.
  • Best Way to Insert or Update Records in Laravel Eloquent — related query-builder pattern for upserts.
  • How to Check If a Record Exists in Laravel — complement to ordering when building list pages.

References

Official Laravel query-builder ordering docs: laravel.com/docs/queries. Eloquent local scopes reference: laravel.com/docs/eloquent.

TAGGED:EloquentLaravelmysqlphpsql

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Laravel user password being updated via artisan tinker with Hash::make How to Change a User Password in Laravel
Next Article Laravel Eloquent multiple where and orWhere — closure-grouped query snippet with parenthesis highlight How to Combine Multiple where() and orWhere() in Laravel Eloquent
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Laravel left outer join — query builder leftJoin illustration with two tables
How to Do a Left Outer Join in Laravel Query Builder
April 20, 2026
Laravel last inserted ID — Eloquent save populates model primary key illustration
How to Retrieve the Last Inserted ID in Laravel Eloquent
April 20, 2026
Laravel Eloquent records today — Carbon today helper and whereDate illustration
How to Get Records Created Today in Laravel
April 20, 2026
Laravel Eloquent current month records — calendar and query builder illustration
How to Get Current Month Records in Laravel Eloquent
April 20, 2026
Laravel Eloquent group by count — Book::groupBy('author') query with bar-chart aggregate icon
How to Count Records Grouped By a Column in Laravel Eloquent
April 20, 2026

You Might Also Like

Fix 409 Conflict error in Laravel (cookies, cache, WAF)
Web Development

How I Fixed the 409 Conflict Error in Laravel (Cookie / Browser / WAF Fix)

7 Min Read
Configure WordPress multisite with subdirectories on Nginx — nginx gear + wordpress tree with subsite branches
Web Development

How to Configure WordPress Multisite with Subdirectories on Nginx

12 Min Read
Check if GD library is installed in PHP (phpinfo and extension_loaded)
Web Development

How to Check if GD Library Is Installed in PHP (3 Easy Methods)

5 Min Read
Send a simple email in Laravel using Mail::raw and SMTP
Web Development

How to Send a Simple Email in Laravel (Fast SMTP + Mail::raw)

4 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?