How to display order by null last in laravel
How to display order by null last in laravel
Through many examples, we will learn how to resolve the "How to display order by null last in laravel".
These code snippets will help to display records in order by null values at last in Laravel. Unfortunately, orderBy() will always put null values first if you are sorting by ascending (ASC) order. But it works fine with descending ( DESC) order in case if you want to put null values at last.-
Order by null values at last using ascending order in laravel 8
$projects = \App\Models\Project::orderByRaw('ISNULL(order_by), order_by ASC')->get(); //Or $projects = \App\Models\Project::orderBy(DB::raw('ISNULL(order_by), order_by'), 'ASC')->get();
You can display records in ascending order with null values at last. To achieve this you should have an order_by column in your project table with an integer type that allows null values.
-
Order by null values at last on pivot table with orderByRaw expression using ascending (ASC) order in laravel
//Method 1 //You can use orderByRaw expression in the model method to sort the records in ascending order with null values at last. </p> public function projects(){ return $this->belongsToMany(Project::class) ->orderByRaw('ISNULL(order_by), order_by ASC') ->withPivot('order_by') ->withTimestamps(); } //Method 2 //You can use the orderByRaw expression in the controller's method to sort records in ascending order with null values at last. //app\Models\<User>.php // Projects method with belongsToMany relation in user model public function projects(){ return $this->belongsToMany(Project::class) ->withPivot('order_by') ->withTimestamps(); } //app\Http\Controllers\<YourController>.php //Controller’s method public function getUserWithProjects(){ $users = User::with(['projects' => function($q){ $q->orderByRaw('ISNULL(order_by), order_by ASC'); }])->first(); return view('user.index', compact('users')); } //resources\views\<user>\<index>.blade.php @foreach($users->projects as $project) {{ $project->name }} {{ $project->pivot->order_by }} <hr> @endforeach
There is a pivot table with an additional column name order_by. The order_by column is an integer column that allows null values. We have to display records in order with the value of 1 in the order_by column should be first, value of 2 should be second and so on.
We can use orderByRaw expression in laravel to display records in ascending order with null values at last.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to send ID to another page in Laravel
- Insert dummy data in users table Laravel
- Send OTP using textlocal api in laravel
- How to insert dynamic value to additional column in pivot table in laravel
- OrderBy on Eloquent relationships method in Laravel
- Laravel get all records with pagination
- Use of undefined constant laravel
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- How to get count of all records created at yesterday
- Check if Relationship Method Exists in Laravel
- Insert Comma Separated Values in laravel
- Get last year created records in Laravel
- Route prefix with auth middleware in laravel
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- Sample configuration files to create laravel project with docker using wsl (window subsystem linux)
- Display first n record from collection in laravel view
- How to check records exist in loaded relationship in Laravel blade view
- In order to use the Auth::routes() method, please install the laravel/ui package
- Link storage folder in laravel 8
- How to check duplicate entry in laravel
- Undefined property: stdClass::$title
- Docker important commands to run laravel application with docker
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- How to pass link from controller to view in laravel on ajax call
- Json encode method in laravel