Laravel Eloquent orderByRaw() Query Example
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, laravel 8, laravel 9, laravel 10 and laravel 11.
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. 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, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent Where Query Examples
- How to Group By with Order By Desc in Laravel?
- Laravel Order By Relation Column Example
- Laravel Multiple Where Condition Example
- Laravel Eloquent WhereIn Query Example
- Laravel Order By Relationship Sum Column Example
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Eloquent Order By Random Row Example
- Laravel Eloquent Where Like Query Example Tutorial
- Laravel Order By with Column Value Example