Hello dev, Today we are going to learn Laravel Order By Multiple Columns Example. This tutorial will cover on how to order by multiple columns with examples in laravel application.
We explained simply about how to order by two columns in laravel. This article goes in detailed on how to use multiple order by in laravel.
You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.
There are several ways to order by with multiple columns in laravel eloquent. We will give you two simple examples using orderBy() and orderByRaw() eloquent method to multiple column order by with “ASC” and “DESC”.
So, let’s see the simple code example:
Examples on Laravel Order By Multiple Columns Example (2022)
Also Read: How to Check If Collection is Empty in Laravel? (2022)
Example 1: Using orderBy()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::select("*")
->orderBy('created_at', 'ASC')
->orderBy('status', 'DESC')
->get();
dd($posts);
}
}
Also Read: How to Get Database Name in Laravel 2022?
Example 2: Using orderByRaw()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::select("*")
->orderByRaw("created_at ASC, status DESC");
->get();
dd($posts);
}
}
Related Question also Ask
- How to order by multiple columns in laravel?
- How do I sort multiple columns in mysql?
- How do I sort a column in laravel?
- Can we use ORDER BY for 2 columns?
- How does ORDER BY multiple columns work?
- How do you use GROUP BY and ORDER BY together?
- Which of the following option can be used for sorting on multiple columns?
- What is necessary to sort by multiple fields?
Conclusion
Today, We had learn Laravel Order By Multiple Columns Example (2022). Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.
Also Read: How to Check Database Connection in Laravel 2022?
[…] Also Read: Laravel Order By Multiple Columns Example (2022) […]
[…] Also Read: Laravel Order By Multiple Columns Example (2022) […]
[…] Also Read: Laravel Order By Multiple Columns Example (2022) […]