Skip to content

digivo/column-sortable

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Column sorting for Laravel 5.2

Latest Version Software License Total Downloads

Package for handling column sorting in Laravel 5.2 and Laravel-5.1.

Simply put: this hack in package with blade extension and Font Awesome icon support.

This is my shot at universal and easy to use model sorting in Laravel. The end result allows you to sort an Eloquent model using any column by clicking the column name. Everything is done in PHP, no JS involved.

Setup

Composer

Pull this package in through Composer.

{
    "require": {
        "kyslik/column-sortable": "~4.0.0"
    }
}
$ composer update

Add the package to your application service providers in config/app.php

'providers' => [
    
    App\Providers\RouteServiceProvider::class,

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

Publish configuration

Publish the package configuration file to your application.

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

See configuration file (config/columnsortable.php) yourself and make adjustments as you wish.

Config in few words

Sortablelink blade extension distinguishes between "types" (numeric, amount, alpha) and applies different class for each of them. See following snippet:

'columns' => [
        'numeric_columns'  => [ 
            'rows' => ['created_at', 'updated_at', 'level', 'id'],
            'class' => 'fa fa-sort-numeric'
        ],
        'amount_columns'   => [
            'rows' => ['price'],
            'class' => 'fa fa-sort-amount'
        ],
        'alpha_columns'    => [
            'rows' => ['name', 'description', 'email', 'slug'],
            'class' => 'fa fa-sort-alpha',
        ],
    ],

Rest of the config file should be crystal clear.

Config update notes

Updating 3.0.* to 3.0.3, use --force option when publishing config file. See this issue, why is config file changed.

Font Awesome support

Install Font-Awesome for visual joy. Search "sort" in cheatsheet and see used icons (12) yourself.

Blade Extension

There is one blade extension for you to use (mind the space between the directive and brackets) fixed in L5.1? (need confirmation)

@sortablelink ('column', 'Title')

Column parameter is order by and Title parameter is displayed inside anchor tags. You can omit Title parameter. In 3.0.3 you can set anchor class in configuration file.

Usage

First of all, include Sortable trait inside your Eloquent model(s). Define $sortable array (see example code below).

Scheme::hasColumn() is run only when $sortable is not defined.

use Kyslik\ColumnSortable\Sortable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

	use Authenticatable, CanResetPassword, Sortable;
	...
	
	protected $sortable = ['id', 
	                       'name', 
	                       'email', 
	                       'created_at', 
	                       'updated_at']; //omitable
	

You're set to go.

Sortable trait adds Sortable scope to the models so you can use it with paginate.

Full Example

####Routes

Route::get('users', ['as' => 'users.index', 'uses' => 'HomeController@index']);

since version 3.0.2 you can use non-named routes

Route::get('users', ['uses' => 'HomeController@index']);

####Controller's index() method

public function index(User $user)
{
    $users = $user->sortable()->paginate(10);

	return view('user.index')->withUsers($users);
}

Since version 3.0.4 you can set default sort (when nothing is in (URL) query strings yet).

//generate ->orderBy('name', 'asc')
$users = $user->sortable(['name'])->paginate(10); //default order is asc

//generate ->orderBy('id', 'desc')
$users = $user->sortable(['id' => 'desc'])->paginate(10);

View

In Laravel 5.2 \Input facade is not aliased by default, to do so open config/app.php and add 'Input' => Illuminate\Support\Facades\Input::class, to aliases array.

pagination included

@sortablelink ('name')

@foreach ($users as $user)
    {{ $user->name }}
@endforeach
{!! $users->appends(\Input::except('page'))->render() !!}

About

Package for handling column sorting in Laravel 5.1 and 5.2

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • PHP 100.0%