Laravel 7/6 - Column sorting with pagination example




Laravel 7/6  - Column sorting with pagination example

Hi Guys,

I always to find something new article for laravel developers. So Today I will learn you column sorting with pagination example. We will sort your table with pagination in laravel.We will do it column sorting using kyslik/column-sortable composer package.

kyslik/column-sortable package provide simple sorting function and provide default icon in this package. They provide sortable() helper for query builder.

I will give you very simple example, so you can easily understand and customize as you want.After end of the tutorial you will get layout like as bellow and easily sorting with each field. So let's just follow.

Step 1 : Install Laravel Application

In this you can install laravel application using bellow command. Bellow command use to install Laravel 6 application.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install kyslik/column-sortable Package

Step 2 You will use bellow command to install kyslik/column-sortable package open your terminal and run bellow command.

composer require kyslik/column-sortable

Successfully install package then after open config/app.php file and add service provider and alias.


'providers' => [





You can publish the default configuration file by following command.

php artisan vendor:publish --provider="Kyslik\ColumnSortable\ColumnSortableServiceProvider" --tag="config"

After run above configuration command, you will find columnsortable.php file in config folder. You can simply change default configuration.

Step 3: Create Product Table and Model

In this step, You will create migration for products table using laravel 6 php artisan command, so fire bellow command.

php artisan make:migration create_products_table

Run this command after you will find migration file database/migrations and you can put the bellow code in 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) {









* Reverse the migrations.


* @return void


public function down()





Migrate table after you can create Product model file. In this model you have to also add sortable facade. So put bellow content in Product.php file:



namespace App;

use Illuminate\Database\Eloquent\Model;

use Kyslik\ColumnSortable\Sortable;

class Product extends Model


use Sortable;

protected $fillable = [ 'name', 'price','details' ];

public $sortable = ['id', 'name', 'price','details', 'created_at', 'updated_at'];


Step 4: Add Route

In this step, You can create route for listing products lists. So open your routes/web.php file and add following route.


Route::get('myproducts', 'ProductController@index');

Step 5: Create ProductController Controller

In this step, You can create new controller Product Controller in this file path app/Http/Controllers/ProductController.php. This controller will manage all listing products and soring helper, so put bellow content in controller file:.



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Product;

class ProductController extends Controller



* Display the products dashboard.


* @return \Illuminate\Http\Response


public function index()


$products = Product::sortable()->paginate(5);

return view('products',compact('products'));



Step 6: Create products View File

In this step, You can create view file for layout and you will write design code here and put following code.


<!DOCTYPE html>



<title>Laravel 6 - Column sorting with pagination example</title>

<link rel="stylesheet" href="">

<link href="" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">



<div class="container">

<h3 class="text-center">Laravel 6 - Column sorting with pagination example</h3>

<table class="table table-bordered">


<th width="80px">@sortablelink('id')</th>







@foreach($products as $key => $product)


<td>{{ $product->id }}</td>

<td>{{ $product->name }}</td>

<td>{{ $product->price }}</td>

<td>{{ $product->details }}</td>

<td>{{ $product->created_at->format('d-m-Y') }}</td>





{!! $products->appends(\Request::except('page'))->render() !!}




Ok finally we got full example, You have insert dummy records in products table thenafter run our example using bellow php artisan command.

php artisan serve

Now you can open bellow URL on your browser:


It will help you...

#Laravel 7


#Laravel 6