Laravel 9 Order By Query Example

Websolutionstuff | Mar-28-2022 | Categories : Laravel PHP MySQL

In this article, we will see laravel 9 order by query example. how to use order by in laravel 9.The orderBy method allows you to sort the results of the query by a given column. The first argument accepted by the orderBy method should be the column you wish to sort by, while the second argument determines the direction of the sort and may be either asc or desc.

Also, I will give you an example of laravel 9 order by with where clause and query of order by in MySQL or multiple order by in one query. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

So, let's see order by in laravel 9, laravel 9  order by multiple, laravel 9 order by desc, how to use order by in laravel 9,  laravel 9 order by with where clause, order by in MySQL

  • SQL Query of Order By
  • Laravel 9 orderBy
  • Laravel 9 orderBy Multiple
  • Laravel 9 orderBy Date Desc
  • Laravel 9 orderBy with Relationship
  • Laravel 9 orderBy with Limit
SQL Query :

MySQL Order By Syntax.

SELECT column1, column2
FROM table_name
ORDER BY column1, column2 ASC|DESC;

Example :

SELECT * FROM users
ORDER BY name;

 

 

Laravel 9 orderBy Query :

In laravel, you can use the orderBy method for ordering records.

$users = DB::table('users')
                ->orderBy('name', 'desc')
                ->get();

 

Laravel 9 orderBy Multiple :

Now, we will see multiple orderBy in a single query.

User::orderBy('name', 'DESC')
    ->orderBy('email', 'ASC')
    ->get();

 

 

Laravel 9 orderBy Date Desc :

In this example, we will see the date orderBy query example.

User::orderBy('created_at', 'DESC')->get();

User::orderBy('updated_at', 'ASC')->get();

 

Laravel 9 orderBy with Relationship :

 For example, your Post model belongs to the User model, and you want to get posts data order by updated_at. You can get it like this. 

$users = User::with(['posts' => function ($query) {
    $query->orderBy('updated_at', 'asc');
}])->get();

 

 

Laravel 9 orderBy with Limit :

The limit methods limit the number of results returned from the query.

$users = User::orderBy('id', 'desc')->limit(10)->get(); 
 
$users = User::orderBy('id', 'asc')->limit(10)->get();

 


You might also like :

Recommended Post
Featured Post
Laravel 8 Export Buttons In Datatables Example
Laravel 8 Export Buttons In Da...

In this article, we will see an example of laravel 8 export buttons in datatables. If you want to export data...

Read More

Oct-14-2020

Laravel 9 One To Many Polymorphic Relationship
Laravel 9 One To Many Polymorp...

In this article, we will see laravel 9 one to many polymorphic relationship. A one-to-many polymorphic relation is...

Read More

Apr-05-2022

Laravel 10 CRUD Operation Step By Step
Laravel 10 CRUD Operation Step...

In this article, we will see how to create a crud operation in laravel 10. Here, we will learn about laravel 10 crud ope...

Read More

Feb-24-2023

How To Create Calendar Event In Laravel 9 Using AJAX
How To Create Calendar Event I...

In this article, we will see how to create a calendar event in laravel 9 using ajax. Here, we will learn how to add...

Read More

Dec-28-2022