Adding Multiple Order By clause in Laravel

2019-12-07 19:28:05 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