
order by multiple columns in Laravel
order by multiple columns in Laravel
In this tutorial, we will try to find the solution to "order by multiple columns in Laravel" through programming.
Sometimes you have to run multiple order by in single query to get the records as per required order. You can use multiple order by in Laravel using orderBy() method on multiple columns.-
OrderBy method on multiple columns in laravel
$users = App\Models\User::orderBy('created_at', 'desc')->orderBy('id', 'asc')->get(); dd($users);
This code snippet will show data in order for columns created_at by DESC and with column id by ASC. It follows the first order by at first and second order by at second priority.
Demo code
Route::get('/order-by', function(){ $users = App\Models\User::orderBy('created_at', 'desc')->orderBy('id', 'asc')->get(); dd($users); });
Just copy/paste code in routes\web.php and visit http://localhost:8000/order-by after running the php artisan serve in command line.
Output :
^ Illuminate\Database\Eloquent\Collection {#1390 ▼ #items: array:14 [▶] }
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
- Redirect to previous page or url in laravel
- How to customize or Change validation error messages
- How to create pivot table in laravel using migration
- On delete set foreign id column value null using migration in laravel 8
- Get today records in Laravel
- Laravel 9 route group with controller
- Touch parent updated_at in Laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- How to call model in blade laravel
- Rendering HTML from database table to view in Laravel
- Automatically remove records using Prunable trait in Laravel
- Create project factory and seed data in laravel
- Send OTP using textlocal api in laravel
- How to validate website url in laravel using validaiton
- Get current URL on visit URL in Laravel
- Redirect from www to non www in laravel using htaccess
- The POST method is not supported for this route. Supported methods: PUT.
- How to check email is valid or not in Laravel
- Delete all related comments on deleting a post in Laravel
- PhpMyAdmin - Error The mysqli extension is missing
- Pass variable from blade to controller Laravel
- Laravel specific table Migration
- How to insert ckeditor data into database in Laravel?
- How to create event and listener in laravel ?