Inside this article we will see the concept i.e How to order by multiple columns in laravel Example Tutorial. Article contains the classified information about How to use multiple order by in laravel Classes.
If you are looking for a solution i.e How to Use Order By for Multiple Columns in Laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.
Read More: How to Create and Use Laravel 9 Macro Example Tutorial
The orderBy
method of laravel allows you to sort the results of the query by a given column. The first argument accepted by the orderBy
method should be the column you wish to sort by, while the second argument determines the direction of the sort and may be either asc
or desc
.
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Create Database & Connect
To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.
CREATE DATABASE laravel_app;
To connect database with application, Open .env file from application root. Search for DB_ and update your details.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=root DB_PASSWORD=root
Example #1: Using orderBy()
Suppose we have a table in database called posts. In that posts table, 2 columns are there – created_at, status.
Read More: Laravel 9 How to get Random Database Records Tutorial
We will do data order by on the basis of these columns.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; class PostController extends Controller { public function index() { $posts = Post::select("*") ->orderBy('created_at', 'ASC') ->orderBy('status', 'DESC') ->get(); dd($posts); } }
Concept
->orderBy('created_at', 'ASC')
->orderBy('status', 'DESC')
We used two consecutive orderBy() methods to add two different conditions.
In laravel, we also have a method to use all order by conditions into raw format.
Let’s see.
Example #2: Using orderByRaw()
The orderByRaw
method used to provide a raw string as the value of the “order by” clause.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; class PostController extends Controller { public function index() { $posts = Post::select("*") ->orderByRaw("created_at ASC, status DESC") ->get(); dd($posts); } }
Concept
->orderByRaw("created_at ASC, status DESC")
We hope this article helped you to learn Laravel 9 Order By Multiple Columns Example Tutorial in a very detailed way.
Read More: CodeIgniter 4 Call Spark Command From Controller Tutorial
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.