In this article, I am going to share with you how to sorting on a column with pagination in laravel 8 using kyslik/column-sortable package. You can do it manualy by you own custom code. But in this example i will use kyslik/column-sortable package to do this.
You know that, sorting is important to find quick and perfect data instant click. If we would like to get ascending date or descending date of creation users then you have to simple sort data and then get. So you can do it in same way if you want for name, price, id etc. So i am here to explain how to do it using kyslik/column-sortable package.
It is easy and interesting thing is that kyslik/column-sortable package provide simple sorting function and they use font Awesome icons for default, you can also customize it. They provide sortable() helper for query builder in your laravel query.
Step 1: Download Laravel App
First, in this step we need to create the laravel application using the following composer command in your terminal. So run this command
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install kyslik/column-sortable Package
Here, we will install kyslik/column-sortable package for sorting table data so open your terminal and run bellow command:
composer require kyslik/column-sortable
After successfully install package, you have to open config/app.php file and add service provider and alias.
config/app.php
'providers' => [
....
Kyslik\ColumnSortable\ColumnSortableServiceProvider::class,
]
You may publish the default configuration file by following command if you want
php artisan vendor:publish --provider="Kyslik\ColumnSortable\ColumnSortableServiceProvider" --tag="config"
Read also : Laravel Sanctum Authentication Example with Product Api
Step 3: Create Product Model
In this step, we need to create the first product table migration using the following artisan command.
php artisan make:model Product -m
After run this command it will generate products table default schema database/migrations and you have to put bellow code in your migration file for create products table.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('details');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
In Product model you have to also add Sortable facade. So put bellow content in Product.php file:
app/Product.php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Kyslik\ColumnSortable\Sortable;
class Product extends Model
{
use Sortable;
protected $fillable = [ 'name', 'details' ];
public $sortable = ['id', 'name', 'details', 'created_at', 'updated_at'];
}
Step 4: Create Route
We almost done. In this is step we have to create route for listing products lists. so open your routes/web.php file and add following route.
routes/web.php
use Illuminate\Http\Request;
use App\Product;
Route::get('products', function(){
$products = Product::sortable()->paginate(5);
return view('products',compact('products'));
});
Step 5: Create products View
We are in final step at end of the tutorial, let's create products.blade.php(resources/views/products.blade.php) for layout and we will write design code here and put following code:
resources/views/products.blade.php
Read also: Create Your Own Custom Log File in Laravel
Hope it can help you.
#laravel #laravel-8x #packages #kyslikcolumn-sortable