Skip to main content
Category:

Laravel Eloquent orderByRaw() Query

 

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();

}

}

 

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();

}

}

 

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();

}

}

 

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();

}

}

 

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();


}

}

 

We are Recommending you

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook