Laravel orderByRaw() Query Example

Laravel orderByRaw() query example; Through this tutorial, i am going to show you how to use orderByRaw() query with model and query builder in laravel apps.

Laravel orderByRaw() Query Example

  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

Let’s see the following example of OrderByRaw() query using eloquent model in laravel apps:

 public function index()
    {
        $users = User::select("*")
                        ->where("status", 1)
                        ->orderByRaw("concat(first_name, ' ', last_name)")
                        ->get();
   
        dd($users);
    }

Example 2: orderByRaw Query using Query Builder

Let’s see the following example of OrderByRaw() query using query builder in laravel apps:

 public function index()
    {
        $users = DB::table('users')->select("*")
                        ->where("status", 1)
                        ->orderByRaw("concat(first_name, ' ', last_name) DESC")
                        ->get();
   
        dd($users);
    }

Example 3: Laravel orderByDesc() Example

Let’s see the following example of orderByDesc() query using query builder in laravel apps:

  public function index()
    {
        $users = User::select("*")
                        ->where("status", 1)
                        ->orderByDesc("name")
                        ->get();
   
        dd($users);
    }

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.