Laravel 7/6 - Column sorting with pagination example

10-Apr-2023

.

Admin

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.

config/app.php

'providers' => [

....

Kyslik\ColumnSortable\ColumnSortableServiceProvider::class,

]

.....

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.

database/migrations/2014_10_12_000000_create_products_table.php

<?php

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->string('price');

$table->text('details');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('products');

}

}

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:

app/Product.php

<?php

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.

routes/web.php

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:.

app/Http/Controllers/ProductController.php

<?php

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.

resources/views/products.blade.php

<!DOCTYPE html>

<html>

<head>

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

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

</head>

<body>

<div class="container">

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

<table class="table table-bordered">

<tr>

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

<th>@sortablelink('name')</th>

<th>@sortablelink('price')</th>

<th>@sortablelink('details')</th>

<th>@sortablelink('created_at')</th>

</tr>

@if($products->count())

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

<tr>

<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>

</tr>

@endforeach

@endif

</table>

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

</div>

</body>

</html>

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:

http://localhost:8000/myproducts

It will help you...

#Laravel 7

#Laravel

#Laravel 6