Laravel 9 OrderBy ASC and DESC Tutorial

In this section, we will look at how to use OrderBy ASC and DESC in Laravel. 5+, 6, 7, 8, 9 can be used.

Also Read : Laravel 9 Seed Multiple Records Tutorial

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:

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

To sort by multiple columns, you may simply invoke OrderBy as many times as necessary:

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

Also Read : Rest API Authentication with Passport Laravel 9

4 Comments

Leave a Reply