kenarkose/sortable

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

Easy sorting for Eloquent models.

1.3.1 2016-08-11 08:28 UTC

This package is not auto-updated.

Last update: 2019-11-04 08:48:33 UTC


README

Easy sorting for Eloquent models.

Build Status Total Downloads Latest Stable Version License

Sortable provides a simple way to sort Eloquent models through model scopes.

Features

  • Compatible with Laravel 5
  • Simple API for sorting Eloquent models through supplied scope
  • Sorting key and direction validation
  • Sortable key configuration, allowing special methods for special keys
  • A phpunit test suite for easy development

Installation

Installing Sortable is simple.

  1. Pull this package in through Composer.

    {
        "require": {
            "kenarkose/sortable": "~1.3"
        }
    }
  2. You may now use the trait and the supplied scope for sorting models.

    
    class Post extends Eloquent
    {
        use Kenarkose\Sortable\Sortable;
    
        protected $sortableColumns = ['title', 'created_at', 'special_key']; // You must define this property
    
        protected $sortableKey = 'title'; // This is optional, default is id
    
        protected $sortableDirection = 'desc'; // This is optional, default is asc
    
        protected $specialSortableKeys = ['special_key', 'specialMethodForSorting'];
        ...
    }
    
    Post::sortable('title', 'asc')->get();
    Post::sortable('title')->get(); // Direction is loaded from request or fallsback to default
    Post::sortable()->get(); // Key and direction are loaded from request or fallback to default
    Post::sortable(null, null)->get(); // Same
    Post::sortable('special-key'); // Calls specialMethodForSorting($query, $key, $direction)
  3. Additionally, you may use the helper method to generate links.

    {{ sortable_link($sortable_key, $link_text_or_content, $link_title }}
    
    <a title="Link Title" class="sortable-link sortable-link--asc" href="http://foo.bar/posts?s=sortable_key&d=asc">Link text or content</a>
    
    // If the supplied key is currently used for sorting the helper toggles direction and adds the active class to the link
    <a title="Link Title" class="sortable-link sortable-link--desc sortable-link--active" href="http://foo.bar/posts?s=current_key&d=asc">Link text or content</a>
    
    // The helper only removes 's', 'd' and 'page' parameters from the previous query string
    <a title="Link Title" class="sortable-link sortable-link--asc" href="http://foo.bar/posts?s=sortable_key&d=asc&keywords=kenarkose&foo=bar">Link text or content</a>

Please check the tests and source code for further documentation, as the source code of Sortable is well tested and documented.

License

Sortable is released under MIT License.