Laravel 8 Order By Query Example

Websolutionstuff | Dec-01-2021 | Categories : Laravel PHP MySQL

In this example we will see laravel 8 order by query example. how to use order by in laravel 8.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 yoy example of laravel 8 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 start how to use order by in laravel 6, laravel 7 and laravel 8.

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

Order By Syntax :

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

 

 

SELECT * FROM users
ORDER BY name;

 

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

 

Laravel orderBy Multiple :

Now, we will see multiple order by in single query.

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

 

Laravel orderBy Date Desc :

In this example we will see date orderBy query example.

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

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

 

 

Laravel orderBy with Relationship :

For example, your Post model belongs to 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 orderBy with Limit :

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

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

 


You might also like :

Recommended Post
Featured Post
How To Fix cURL Error 60 SSL Certificate Problem
How To Fix cURL Error 60 SSL C...

In this example we see how to fix cURL error 60 SSL certificate problem. cURL error 60: SSL certificate proble...

Read More

Dec-08-2021

How to Install Jenkins on Ubuntu
How to Install Jenkins on Ubun...

In today's fast-paced software development landscape, I've understood the significance of continuous integration...

Read More

Aug-07-2023

The Best Laravel Tips & Tricks for Migrations
The Best Laravel Tips & Tricks...

Migrations are an essential part of any Laravel project, allowing developers to easily manage and update their database...

Read More

Oct-23-2023

How To Create Pie Chart In Laravel 9 Using Highcharts
How To Create Pie Chart In Lar...

In this article, we will see how to create a pie chart in laravel 9 using highcharts. A pie chart is a circular statisti...

Read More

Oct-05-2022