Site Admin
Site Admin Founder of MeaningArticles
923 Views

Laravel 5 Handle Column Sorting

Hello Dev.

We've new package kyslik/column-sortable [https://github.com/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.
app.php

'providers' => [

    App\Providers\RouteServiceProvider::class,

    /*
     * Third Party Service Providers...
     */
    Kyslik\ColumnSortable\ColumnSortableServiceProvider::class,
],

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.
User.php

<?php
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',
                        'name',
                        'email',
                        'created_at',
                        'updated_at'];
}
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.

<?php

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


Step 6: Create controller

Create controller file to hanle route.
UserController.php

<?php

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.
users.blade.php

<!DOCTYPE html>
<html>

<head>
    <title>Column sorting Example - meaningarticles.com</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">
</head>

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

</html>

Now run the project with below command

php artisan serve

Open route link

http://localhost:8000/users

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