c6digital / laravel-orderable
为 Laravel 模型提供直观的排序。
v1.0.0
2024-03-18 16:23 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
This package is auto-updated.
Last update: 2024-09-08 01:47:39 UTC
README
此包提供了一个简单的 Orderable
特性,您可以在您的 Eloquent 模型上实现它。它注册了一些作用域,允许您对模型进行排序,并在创建后处理模型的重新排序。
安装
您可以通过 Composer 安装此包
composer require c6digital/laravel-orderable
用法
在您的模型上注册 Orderable
特性。
use C6Digital\Orderable\Concerns\Orderable; use C6Digital\Orderable\Concerns\OrderableOptions; class Post extends Model { use Orderable; public static function getOrderableOptions(): OrderableOptions { return OrderableOptions::default() ->column('order') // Set the column that is used to order this model. ->reorderOnCreate() // Determine whether models should be re-ordered after creation. ->moveToEndOnCreate() // Place newly created models at the end. ->moveToStartOnCreate() // Place newly created models at the start. ->setOrderUsing(callback: ...); // Set the order manually using a custom callback. } }
默认列是
order
,请确保将其添加到您的数据库表中。该包不会自动为您完成此操作。
使用 ordered()
、orderedAsc()
和 orderedDesc()
方法在查询中排序结果。
Post::query() ->ordered() // Defaults to `ASC`. ->orderedAsc() // Orders `ASC`. ->orderedDesc() // Orders `DESC`.
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请审查 我们的安全策略 了解如何报告安全漏洞。
致谢
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。