咨询/laravel-sorting

laravel 的排序包

0.1.7 2023-01-23 15:43 UTC

This package is auto-updated.

Last update: 2024-08-23 18:56:48 UTC


README

一套用于在 Laravel 中对模型进行排序的实用工具。

安装

使用 composer 安装: composer require konsulting/laravel-sorting

用法

排序

  • Konsulting\Laravel\Sorting\Sortable 特性添加到您的模型中。
  • 通过添加 $sortableSettings 属性来设置模型的配置,提供允许排序的字段和默认排序顺序。
protected static $sortableSettings = [
    'sortable' => ['name', 'created_at', 'updated_at'],
    'defaultSort' => '+name',
];
  • 在您想添加可排序链接的视图中,包含以下内容(以下为 Post 模型的示例)
{{ App\Post::sortableLink('name', 'Name') }}
  • 当您想检索排序后的模型集合时,使用 sort() 方法。
App\Post::sort()->paginate();
// The sort method will extract the sort variable from the request, unless you pass them through (e.g. if you store in the session).

优先级

  • Konsulting\Laravel\Sorting\Prioritise 特性添加到您的模型中。

  • 确保您的模型有一个名为 'priority' 的列。默认情况下,这被称为 'priority',但可以在模型中通过重写 determinePriorityColumnName() 方法来覆盖。

  • 使用 prioritise 范围来按优先级排序检索到的结果。

安全

如果您发现任何安全问题或有任何疑问,请通过电子邮件 keoghan@klever.co.uk 联系我们,而不是使用问题跟踪器。

贡献

欢迎贡献,并将得到充分认可。我们将通过 Pull Request 接受贡献。

  • 使用 PSR-2 编码标准
  • 添加测试,如果您不确定如何操作,请提问。
  • 在 readme.md 中记录行为变化。

测试

我们使用 PHPUnit

使用 PHPUnit 运行测试: vendor/bin/phpunit