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

Laravel multiple orderBy clause with example

Aman Jain, October 6, 2022March 16, 2024

In this blog post, we will take a look at how to use the Laravel multiple orderBy clause in the Eloquent ORM. You can order query results by multiple columns using the orderBy method. This is useful when you want to sort by multiple criteria. In this post, we’ll show you how to use the orderBy method to sort query results by multiple columns

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.

Laravel multiple orderBy example

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 – 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 laravelmultipleorderby

Post navigation

Previous post
Next post

Related Posts

Php How to Restore Soft Deleted Records in Laravel 9

How to Restore Soft Deleted Records in Laravel 9 ?

June 16, 2022June 15, 2022

In this article we will learn to restore soft deleted records in Laravel. In our recent article How to fetch Soft Deleted Records in Laravel 9 ? we learnt to fetch the records from database which is soft deleted and sometimes we want to restore records that are soft deleted…

Read More
Php Laravel 11 CRUD Example Tutorial with Search, Image and Pagination

Laravel 11 CRUD Example Tutorial with Search, Image and Pagination

July 5, 2024July 5, 2024

In this article, we’ll delve into Laravel and explore the implementation of CRUD operations alongside essential features like Search, Image uploading, and Pagination. CRUD, an acronym for Create, Read, Update, and Delete, forms the backbone of database management in web applications across all programming paradigms. This tutorial covers these fundamental…

Read More
Laravel 9 Collection Count and Check Empty Eloquent Collection in Laravel

Collection Count and Check Empty Eloquent Collection in Laravel

October 26, 2022March 16, 2024

In this article we will learn collection count and detect or check empty eloquent collection in laravel. Laravel collection is used to simplify the operations of array. Illuminate\Support\Collection library is used to handle the collection and arrays in laravel. Laravel heavily used collections in eloquent query builder to result the…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version