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

How to enable cors in web api php or laravel?

August 27, 2021August 20, 2022

Cross-Origin Resource Sharing (CORS) is mechanism which allow browser to share the resources between the other domain or port. if the cors is disabled in api or server then other domain can’t access the apis and resource of server. Example: if a server A wants to access the apis of server…

Read More
Php How to Show Success and Error Flash Message in Laravel

How to Show Success and Error Flash Message in Laravel 10 ?

March 26, 2023March 16, 2024

This article aims to teach how to display flash messages indicating success or error in Laravel 10. Flash messages come in handy when notifying users of recent activities such as form submissions or website actions. In such cases, multiple types of messages may be required. Sometimes, error messages need to…

Read More
Php How to Get Only Soft Deleted Records in Laravel 9

How to Get Only Soft Deleted Records in Laravel 9 ?

June 20, 2022June 20, 2022

In this article we will learn to get only soft deleted records in Laravel. In our recent article Use Soft Delete to Temporary (Trash) Delete the Records in Laravel 9 we learnt to delete the file without actually deleting from database and sometimes we want to show only that records…

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