Sorting data in a table is a fundamental feature for any web application dealing with large datasets. Laravel, a powerful PHP framework, makes it easy to implement column sorting functionality in your web applications.
In this article, we'll walk you through the process of sorting columns in a table using Laravel 10. We will use kyslik/column-sortable laravel package for sorting columns in the table.
Using the kyslik/column-sortable package you can simple way to sort any columns into the database.
Here's a step-by-step guide to adding column sorting to your Laravel 10 application:
In this step, we will install laravel 10 using the composer command.
laravel new column-sorting-app
Now, we will install sortable package using the following command.
composer require kyslik/column-sortable
Configure the Package
In your Laravel project, open the config/app.php
file, and add the following service provider and alias to the respective arrays:
'providers' => [
// ...
Kyslik\ColumnSortable\ColumnSortableServiceProvider::class,
],
'aliases' => [
// ...
'ColumnSortable' => Kyslik\ColumnSortable\Facades\ColumnSortable::class,
],
Configure your database connection in the .env
file with the appropriate database credentials.
Now, we will create model and migration using the following command.
php artisan make:model Item -m
Open the generated migration file located in the database/migrations
directory and define the schema for your table.
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('price');
$table->timestamps();
});
}
In the model associated with the table you want to sort, use the ColumnSortable
trait.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Kyslik\ColumnSortable\Sortable;
class Item extends Model
{
use Sortable;
protected $fillable = ['name', 'price', 'created_at'];
public $sortable = ['name', 'price', 'created_at'];
}
In the above code, we've defined the columns that can be sorted in the $sortable
array.
Run the following command to migrate the table into the database.
php artisan migrate
Generate a controller if you haven't already.
php artisan make:controller ItemController
In the controller, retrieve the data from the database and apply sorting.
use App\Models\Item;
use Illuminate\Http\Request;
use ColumnSortable;
class ItemController extends Controller
{
public function index(Request $request)
{
$items = Item::sortable()->paginate(10); // Apply sorting and paginate the results
return view('items.index', compact('items'));
}
}
Create a Blade view to display your table. In the table headers, use the sortable()
method to generate sorting links for each sortable column.
<h2>Sorting Column with Pagination in Laravel 10 - Vidvatek</h2>
<table>
<thead>
<tr>
<th>{!! ColumnSortable::link('name') !!}</th>
<th>{!! ColumnSortable::link('price') !!}</th>
<th>{!! ColumnSortable::link('created_at') !!}</th>
</tr>
</thead>
<tbody>
@foreach($items as $item)
<tr>
<td>{{ $item->name }}</td>
<td>{{ $item->price }}</td>
<td>{{ $item->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $items->appends(request()->except('page'))->links() }}
Define a route in routes/web.php
to access the index
method of your controller.
Route::get('/items', 'ItemController@index');
By following these steps, you can easily implement column sorting in a Laravel application using the kyslik/column-sortable
package. This package simplifies the process of adding sorting functionality to your tables, enhancing the user experience when working with large datasets
You might also like:
- Read Also: 10 Reasons Why Laravel Is the Best PHP Framework 2023
- Read Also: How To Integrate Paypal Payment Gateway In Laravel 10
- Read Also: How to Read Excel File in Python using Openpyxl
- Read Also: Laravel 10 many to many Relationship Example