kenarkose/sortable

此包已弃用且不再维护。未建议替代包。

为Eloquent模型提供简单的排序功能。

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

为Eloquent模型提供简单的排序功能。

Build Status Total Downloads Latest Stable Version License

Sortable提供了一种简单的方法,通过模型作用域对Eloquent模型进行排序。

功能

  • 与Laravel 5兼容
  • 简单的API,通过提供的范围对Eloquent模型进行排序
  • 排序键和方向的验证
  • 排序键配置,允许对特殊键使用特殊方法
  • 一个phpunit测试套件,便于开发

安装

安装Sortable很简单。

  1. 通过Composer安装此包。

    {
        "require": {
            "kenarkose/sortable": "~1.3"
        }
    }
  2. 您现在可以使用特性和提供的范围对模型进行排序。

    
    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. 此外,您还可以使用辅助方法生成链接。

    {{ 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>

请检查测试和源代码以获取更多文档,因为Sortable的源代码经过良好测试和文档化。

许可证

Sortable在MIT许可证下发布。