Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel orderBy clause in eloquent with example

Laravel orderBy clause in eloquent with example

Aman Jain, October 5, 2022March 16, 2024

In this blog post, we will take a look at how to use the Laravel orderBy clause in the Eloquent ORM.Laravel provides an expressive, fluent interface for creating and retrieving records in your database.

When we work with databases in Laravel, we often need to order the results we get back from the query. For example, we may want to get all the articles in alphabetical order, or we may want to get all the products in order of price. Laravel makes this easy to do with the orderBy clause.

The orderBy clause takes two parameters: the column to order by and the direction to order in. The direction can be either ‘asc’ for ascending or ‘desc’ for descending.

Let’s take a look at Laravel orderBy clause in eloquent with example. Say we have a articles table with the following columns: id, title, email, created_at. We can use the following query to get all the articles in alphabetical order:

$articles = Article::orderBy('title', 'asc')->get();

This will give us an array of Articles objects, ordered by the title column in ascending order.
If we want to get the articles in reverse alphabetical order, we can change the direction to ‘desc’:

$articles = Article::orderBy('title', 'desc')->get();

Laravel orderby multiple columns

We can also order by multiple columns. For example, if we want to get the users in alphabetical order, but order by created_at in reverse order for those with the same name:

$articles = Article::orderBy('title', 'desc')->orderBy('created_at', 'desc')->get();

This can be useful if you want to show name alphabetically with recent articles.

Example 1 – Laravel single column orderBy

So in this example i will use laravel query builder or eloquent single column orderBy .

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    { 
        $sortBy=$request->get("sortBy");

        $Article = Article::orderBy('title', 'asc')->get();
   }
}

Output will be if sortBy is title :

select * from `articles` where order by title asc

Example 2 – Laravel multiple column orderBy

So in this example i will use laravel query builder or eloquent multiple column orderBy .

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    { 
        $sortBy=$request->get("sortBy");

       $articles = Article::orderBy('title', 'desc')->orderBy('created_at', 'desc')->get();
   }
}

Output will be if sortBy is title :


select * from `articles` order by title DESC, created_at desc

Also Read : How to use conditional orderBy clause in Laravel 8 eloquent ?

Related

Php Laravel laravelmultipleorderbysingle

Post navigation

Previous post
Next post

Related Posts

Php How to Use hasOne Relationship in Laravel

How to Use hasOne Relationship in Laravel with Example ?

February 27, 2022October 4, 2022

hasOne relationship in laravel is used to create the relation between two tables. hasOne means create the relation one to one. For example if a article has comments and we wanted to get one comment with the article details then we can use hasOne relationship or a user can have…

Read More
Php How to Send Mail in Laravel 8 : 9 to multiple recipients, CC and without SMTP

How to Send Mail in Laravel 8 / 9 to multiple recipients, CC and without SMTP ?

May 11, 2022May 11, 2022

In this article i will show you to Send Mail in Laravel to multiple recipients, without view and without SMTP. We can send our mail to multiple recipients using the same to method with array collection with email and name. if you do not want to use SMTP then you…

Read More
Php How to compress and reduce image size while uploading in laravel

How to compress and reduce image size while uploading in laravel 9 ?

May 14, 2022May 15, 2022

While uploading the images in our application user can upload big size image but storing and rendering big size of image can reduce the performance of the application therefor its an important aspect to reduce the size of image without resizing it so we can keep the original size and…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version