
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
- Laravel onclick function not working
- Route [password.request] not defined
- Pass variable from blade to controller Laravel
- Route not defined in Laravel
- How to create laravel project using composer
- How to get the random value form a specific column in laravel ?
- The use statement with non-compound name 'Auth' has no effect
- How to get random string in Laravel
- How to get images from AWS s3 and display in Laravel blade
- How to upload multiple images after preview in laravel using cropper js
- Run artisan command to generate key in laravel
- Seed database using SQL file in Laravel
- How to restore multiple records after soft-deletes in Laravel
- Create records using relationship in laravel
- Laravel route redirect not working
- Get only 10 records from table in laravel
- Laravel get count with where condition
- Submit form without CSRF token in Laravel
- Ajax POST request in laravel
- Update if exist else insert new record in laravel
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- How to get IP address in laravel
- Multiple Level eager loading in Laravel
- How to check data inserted or deleted in pivot after toggle method
- Automatically remove records using Prunable trait in Laravel