In Laravel 9, there are multiple ways to get data by order. One way is shown below:
Today, we will learn how to use orderBy in Laravel and when to use it.
orderBy in Laravel
In Laravel 9, when we need to sort our data collection by descending or ascending order from the database. Then we need to use an orderBy in the Laravel query. In the regular MySQL queries, we use it as shown below:
But, Laravel has a different way of assigning the following:
Project requirements are given below:
- MySQL 8.0+
- MariaDB 10.2+
- PHP 8.1
Here is an example of defining the orderBy query:
- Process1. Create an orderBy Project
- Process 2. Database Connection
- Process 3. Apply the orderBy Method
- Process 4. Run and Test the orderBy Project
Process 1. Create an OrderBy Project
Now, we need to run this command to create this project:
Process 2. Database Connection
Open the .env file on the orderByProject project and add new database details.
Here’s the following code:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Database name
DB_USERNAME=Database user name
DB_PASSWORD=Database password
Check the following image:
Process 3. Apply the orderBy Method
Before we apply orderBy to our project, we will need to create a database table for our database. For that, we will create a table called “CollectionList”. We have to run the following command to create the table:
The code is provided below:
{
public function up()
{
Schema::create('collection_lists', function (Blueprint $table) {
$table->id(); //auto increment
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('collection_lists');
}
};
I need to add these two to the following code:
$table->longText('details')->nullable();
Let’s migrate the data to the database. Run the following command:
Next, create a controller to manage the function with the query.
Here, we created a controller named “CollectionList” for our OrderBy project. We need to run this command to create the following project:
The code should look like this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CollectionList extends Controller
{
//
}
Now, I need to create a function in the controller:
The name of the function isallCollection.
After adding this function, it should look as follows:
namespace App\Http\Controllers;
use App\Models\CollectionList as ModelsCollectionList;
use Illuminate\Http\Request;
class CollectionList extends Controller
{
public function allCollection()
{
$alldata = ModelsCollectionList::orderBy("id", "asc")->get();
return view('welcome',compact('alldata'));
}
}
For the get() method, we need to use data in ascending order:
get();
To get the data in descending order, we need to use the following:
Add a view under the resource\View folder called welcome.blade.php.
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<head>
<div class="container">
<div class="row">
<div class="col-md-12 text-right">
</div>
</div>
@foreach($alldata as $each_article)
<div class="col-md-12">
<h1>{{$each_article->name}}</h1>
<hr>
</div>
@endforeach
</div>
</html>
Now, we need to add a route in routes\web.php:
Process 4. Run and Test the orderBy Project
Next, we need to run the following:
For the ascending order, the project will look like the following image:
For the descending order, it looks like the following image:
Conclusion
In this article, we created this Laravel Query project with Laravel 9. Creating a data table Laravel orderBy is very useful. We hope this orderBy project example will help you to understand the orderBy in Laravel 9.