
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 URL validation not working
- How to get CSRF token in laravel controller
- How to restore multiple records after soft-deletes in Laravel
- Conditional validation in laravel
- Print last executed query in laravel
- Check if Relationship Method Exists in Laravel
- Get posts belongs to a specific user in Laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- How to get file extension from input type file in laravel
- Composer\Exception\NoSslException
- How to pass data to route in laravel?
- How to check records exist in loaded relationship in Laravel blade view
- How to check query string exists or not in laravel blade
- Send post data from controller to view
- Laravel specific table Migration
- Session Doesn't Work on Redirect
- Touch parent updated_at in Laravel
- How to make Copy or Duplicate table row in laravel
- Get the post details if it has at least one comment in comments table
- Route not defined in Laravel
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- How to create static page in Laravel
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- How to display HTML tags In Laravel blade
- Laravel delete all rows older than 30 days