A package to sort results based on a query string in the URL
No longer maintained, use https://github.com/MacsiDigital/laravel-sortable
A simple package to sort results by any field on an eloquent model, including by joins. Uses the Laravel default query builder order and join functions.
You can install the package via composer:
composer require macsidigital/sortable
Create 2 arrays in your elequent model, the first with the fields that can be sorted, the second showing any table joins.
protected $sortable = [ 'name', 'email', 'addresses.country' ]; protected $extended_joins = [ 'addresses.country' => [ 'table_field' => 'users.id', 'foreign_table_field' => 'addresses.addressable_id', 'restrict_table_field' => 'addresses.addressable_type', 'restrict_value' => 'App\User' ] ];
If no table joins are required then you will only need the sortable array.
For any joins include the table and field seperated by a period (.).
Then to sort the fields simply add a sortable() and pass in any fields as an array to sort the results. This has to be in the query builder prior to any get/first requests.
Use the table blade helper to output sortable links, the first argument is the column name and the 2nd is an array of any key value pairs to include in the anchor tag, if a label is given it will override the column name.
<tr> <th>@sortablecolumnlink('name')</th> <th>@sortablecolumnlink('email')</th> <th>@sortablecolumnlink('addresses.country', ['label' => 'Country', 'class' => 'country])</th> </tr>
Finally you can set icons in the config file, by default fontawesome icons are used, but this can easily be changed to whatever library you want to use.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email email@example.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.