Thursday, September 4, 2025
HomeLanguagesLaravel orderByRaw() Query Example

Laravel orderByRaw() Query Example

orderByRaw() query in laravel; In this tutorial, you will learn in detail how to write and use query using orderByRaw(), select raw and select DB raw in laravel with eloquent join.

You can use the laravel orderByRaw eloquent method to building query in laravel apps. And also use laravel select raw with multiple conditions in eloquent queries.

So, let’s see the following examples that will help you on how to use orderByRaw() eloquent query in laravel:

  1. Example 1: Laravel OrderByRaw Query using Model
  2. Example 2: orderByRaw Query using Query Builder
  3. Example 3: Laravel orderByDesc() Example

Example 1: Laravel OrderByRaw Query using Model

<?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);
    }
}

Dump the above given orderByRaw query you will get the following SQL query:

select * from `users` 

    where `status` = ? 

    order by concat(first_name, ' ', last_name)

Example 2: orderByRaw Query using Query Builder

<?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 = DB::table('users')->select("*")
                        ->where("status", 1)
                        ->orderByRaw("concat(first_name, ' ', last_name) DESC")
                        ->get();
  
        dd($users);
    }
}

Dump the above given orderByRaw query you will get the following SQL query:

select * from `users` 

    where `status` = ? 

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

Example 3: Laravel orderByDesc() Example

<?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);
    }
}

Dump the above given orderByRaw query you will get the following SQL query:

select * from `users` 

    where `status` = ? 

    order by `name` desc

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

RELATED ARTICLES

Most Popular

Dominic
32262 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11856 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS