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.
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 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.
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.
Updating 3.0.* to 3.0.3, use
--forceoption when publishing config file. See this issue, why is config file changed.
Install Font-Awesome for visual joy. Search "sort" in cheatsheet and see used icons (12) yourself.
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.
First of all, include Sortable trait inside your Eloquent model(s). Define $sortable array (see example code below).
Scheme::hasColumn()is run only when$sortableis 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.
####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);
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() !!}