Laravel Eloquent orderByRaw() Query Example

By Hardik Savani July 21, 2020 Category : Laravel

This tutorial will provide example of laravel orderbyraw example. you can see orderbyraw query in laravel example. you can understand a concept of laravel first name and last name order by example. I’m going to show you about orderbyraw field in laravel 6, laravel 7 and laravel 8.

In this example i will give you very simple example of how to use orderByRaw in laravel application. you can easily use it with laravel 6 and laravel 7 application. sometime we need to use order by first_name and last_name. we can not do it easily but we can do it with using concat function of sql. same thing you can also use other sql function with orderByRaw.

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

Example 1: 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 2: 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);

}

}

Example 3: 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 4: 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 5: 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);

}

}

I hope it can help you...