Laravel Eloquent Order By Query Example
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...
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
- How to Group By with Order By Desc in Laravel?
- Laravel Order By Relation Column Example
- How to use whereHas with orWhereHas in Laravel?
- Laravel Order By Relationship Sum Column Example
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel 5.4 New Feature - Add Eloquent WhereKey Method Example
- Laravel - whereDate(), whereMonth(), whereDay() and whereYear() Example
- Laravel Where Condition with Two Columns Example
- Laravel Where Clause with date_format() Example
- Laravel Eloquent Order By Random Row Example
- Laravel Eloquent Where Like Query Example Tutorial
- Laravel Order By with Column Value Example