Site Admin
Site Admin Founder of MeaningArticles

Laravel 5 Handle Column Sorting

Hello Dev.

We've new package kyslik/column-sortable [] for Laravel to handle column sorting. With the assist of this package, you may sort the column in ascending or descending order. this is beneficial in Admin panel where there are masses of information displaying.

So shall we start building the application. we are able to go through little by little guide.

So let's start the lesson...

Step 1: Create new Laravel project

Within the first step, we have created new Laravel project the usage of below command

composer create-project laravel/laravel column

Step 2: Install package

Within the next step we will install the kyslik/column-sortable package inside the utility.

composer require kyslik/column-sortable

Step 3: Configure package

Now open Aad the service provider to array of providers in config/app.php.

'providers' => [


     * Third Party Service Providers...

publish the configuration file for kyslik/column-sortable package in Laravel. this may upload configuration file config/columnsortable.php to make modifications if you need.

Step 4: Add Trait

Now, add Sortable trait to the User.php model and add $sortable variable.

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Kyslik\ColumnSortable\Sortable;

class User extends Model implements
    use Authenticatable, Sortable;

    public $sortable = ['id',
php artisan vendor:publish --provider="Kyslik\ColumnSortable\ColumnSortableServiceProvider" --tag="config"

Step 5: Add route

Add route to show user data table so that we can sorting features.


Route::get('users', '[email protected]');

Step 6: Create controller

Create controller file to hanle route.


namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
     * list all users.
     * @return \Illuminate\Http\Response
    public function index()
        $users = User::sortable()->paginate(10);

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

Step 7: Create blade file

Finally create view file resource/views/users.blade.php and add below code in that file.

<!DOCTYPE html>

    <title>Column sorting Example -</title>
    <link rel="stylesheet" href="">
    <link href="" rel="stylesheet">

    <div class="container">
        <h3 class="text-center">Laravel column sorting -</h3>
        <table class="table table-bordered">
                <th>@sortablelink('created_at','Created At')</th>
                @foreach($users as $user)
                        <td>{{ $user->id }}</td>
                        <td>{{ $user->name }}</td>
                        <td>{{ $user->email }}</td>
                        <td>{{ $user->created_at->diffForHumans() }}</td>
        {!! $users->appends(\Request::except('page'))->render() !!}


Now run the project with below command

php artisan serve

Open route link


I hope it assists you, thanks for visiting my article if you like my article then share it with your friends on the social media platform.

Happy Coding.....