Adding Multiple Order By clause in Laravel

2024-04-27 06:22:56 Sushant Laravel

How to use order By clause in Laravel?

Order by is used to sort sql results by column names. In this example we will see how to add multiple orderby clause in Laravel.

Let take an example we have a users table and we want to sort results by name of user, then here is query to do that in Laravel.


User::where(['status'=>'active'])->orderBy('name')->get()


The above Eloquent query return all user those status is active in sorted ascending order by name. To sort in descending order use orderBy('name','DESC').

Similarly you can add as many order by clause in Laravel. Here is an example.

User::where(['status'=>'active'])->orderBy('name')->orderBy('age','DESC')->orderBy('created_at')->get()



This post is submitted by one of our members. You may submit a new post here.

Related Tricks