This package is abandoned and no longer maintained. No replacement package was suggested.

A package to sort results based on a query string in the URL

1.0.9 2020-06-18 15:22 UTC

This package is auto-updated.

Last update: 2021-04-23 12:33:32 UTC


Latest Version on Packagist Build Status StyleCI Scrutinizer Code Quality Total Downloads

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/laravel-sortable

The service provider should automatically register for For Laravel > 5.4.

For Laravel < 5.5, open config/app.php and, within the providers array, append:



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', ''

protected $extended_joins = [
    '' => [
        'table_field' => '',
        '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.

Users::sortable(explode(',', request()->query('sort')))->get();

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.

    <th>@sortablecolumnlink('', ['label' => 'Country', 'class' => 'country])</th>

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 instead of using the issue tracker.



The MIT License (MIT). Please see License File for more information.