Laravel Eloquent Order By Query Example


Share:
Published By: Admin, Published On: May 08, 2021, Category: Laravel

Hello Friends,

Now let's see example of how to use order by in laravel. This is a short guide on laravel if order by query. We will use how to use order by in laravel. Here you will learn how to use order by in laravel. We will use how to use if order by in laravel. Let's get started with how to order by use in laravel.

Here i will give you many example how you can check order by query in laravel.

Example 1: Laravel orderBy() Example

Laravel Query:

<?php
  
	namespace App\Http\Controllers;
	  
	use Illuminate\Http\Request;
	use App\Admin;
	  
	class AdminController extends Controller
	{
	    /**
	     * Display a listing of the resource.
	     *
	     * @return \Illuminate\Http\Response
	     */
	    public function index()
	    {
	        $admins = Admin::select("*")
	                        ->where("status", 1)
	                        ->orderBy("name")
	                        ->get();
	  
	        dd($admins);
	    }
	}
?>
Example 2: Laravel orderBy() with DESC Example

Laravel Query:

<?php
  
	namespace App\Http\Controllers;
	  
	use Illuminate\Http\Request;
	use App\Admin;
	  
	class AdminController extends Controller
	{
	    /**
	     * Display a listing of the resource.
	     *
	     * @return \Illuminate\Http\Response
	     */
	    public function index()
	    {
	        $admins = Admin::select("*")
	                        ->where("status", 1)
	                        ->orderBy("name", "desc")
	                        ->get();
	  
	        dd($admins);
	    }
	}
?>
Example 3: Laravel orderByDesc() Example

Laravel Query:

<?php
  
	namespace App\Http\Controllers;
	  
	use Illuminate\Http\Request;
	use App\Admin;
	  
	class AdminController extends Controller
	{
	    /**
	     * Display a listing of the resource.
	     *
	     * @return \Illuminate\Http\Response
	     */
	    public function index()
	    {
	        $admins = Admin::select("*")
	                        ->where("status", 1)
	                        ->orderByDesc("name")
	                        ->get();
	  
	        dd($admins);
	    }
	}
?>
Example 4: Laravel orderByRaw() Example

Laravel Query:

<?php
  
	namespace App\Http\Controllers;
	  
	use Illuminate\Http\Request;
	use App\Admin;
	  
	class AdminController extends Controller
	{
	    /**
	     * Display a listing of the resource.
	     *
	     * @return \Illuminate\Http\Response
	     */
	    public function index()
	    {
	        $admins = Admin::select("*")
	                        ->where("status", 1)
	                        ->orderByRaw("concat(surname, ' ', name)")
	                        ->get();
	  
	        dd($admins);
	    }
	}
?>
Example 5: Laravel orderByRaw() with DESC Example

Laravel Query:

<?php
  
	namespace App\Http\Controllers;
	  
	use Illuminate\Http\Request;
	use App\Admin;
	  
	class AdminController extends Controller
	{
	    /**
	     * Display a listing of the resource.
	     *
	     * @return \Illuminate\Http\Response
	     */
	    public function index()
	    {
	        $admins = Admin::select("*")
	                        ->where("status", 1)
	                        ->orderByRaw("concat(surname, ' ', name) DESC")
	                        ->get();
	  
	        dd($admins);
	    }
	}
?>

It will help you....

Share: