Laravel Eloquent Order By Query Example

By Hardik Savani | July 21, 2020 | 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 laravel orderbydesc example.

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...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: