Laravel Eloquent Order By Query Example

By Hardik Savani April 16, 2024 Category : Laravel

Now, let's see post of laravel eloquent order by example. you can see laravel eloquent order by desc. it's simple example of laravel sort by query example. This article goes in detailed on order by in laravel eloquent.

Let's see bellow example of orderbydesc in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

In this example i will give you example of how to use sorting query in laravel application. you can easily use it with laravel 6 and laravel 7 application. sometime we need to use order by with ascending and descending.

So, let's see bellow examples that will help you how to use order by eloquent query in laravel.

Example 1: Laravel orderBy() Example

SQL Query:

select * from `users`

where `status` = ?

order by `name` asc

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->where("status", 1)

->orderBy("name")

->get();

dd($users);

}

}

Example 2: Laravel orderBy() with DESC Example

SQL Query:

select * from `users`

where `status` = ?

order by `name` desc

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->where("status", 1)

->orderBy("name", "desc")

->get();

dd($users);

}

}

Example 3: Laravel orderByDesc() Example

SQL Query:

select * from `users`

where `status` = ?

order by `name` desc

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->where("status", 1)

->orderByDesc("name")

->get();

dd($users);

}

}

Example 4: Laravel orderByRaw() Example

SQL Query:

select * from `users`

where `status` = ?

order by concat(first_name, ' ', last_name)

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->where("status", 1)

->orderByRaw("concat(first_name, ' ', last_name)")

->get();

dd($users);

}

}

Example 5: Laravel orderByRaw() with DESC Example

SQL Query:

select * from `users`

where `status` = ?

order by concat(first_name, ' ', last_name) DESC

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->where("status", 1)

->orderByRaw("concat(first_name, ' ', last_name) DESC")

->get();

dd($users);

}

}

I hope it can help you...

Shares