Laravel Eloquent orderByRaw() Query Builder Example

Laravel orderByRaw Query Example; In this tutorial you will lean how to use orderByRaw() query in laravel application.

laravel orderByRaw query ordering the records with concenate two columns. Below we are using created_at and updated_at with conctnate and use the order by with raw query.

Let’s see the below example for how to easy way to use orderbyraw eloquent query in laravel 6, laravel 7 or laravel 8.

Example 1:

$orders = DB::table('orders')
           ->orderByRaw('updated_at - created_at DESC')

Example 2:

use App\Models\User;

class UserController extends Controller
    public function index()
        $users = User::where("status", 1)
                    ->orderByRaw("concat(first_name, ' ', last_name) DESC")

I hope its work for you..

Leave a Comment