creativecrafts / laravel-sort-collection
一个简单的包,用于排序查询集合。
1.2.0
2024-03-18 08:19 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^11.0|^10.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1|^7.0
- orchestra/testbench: ^9.0|^8.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
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-09-08 19:41:45 UTC
README
一个简单实用的包,用于排序查询集合。
安装
您可以通过composer安装此包
composer require creativecrafts/laravel-sort-collection
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="sort-collection-config"
这是已发布配置文件的内容
return [ /** * The default sort direction to use when sorting a collection. * supported: asc, desc */ 'sort_direction' => 'desc', ];
可选地,您可以使用以下命令发布视图
用法
use CreativeCrafts\SortCollection\Sort; // simple collection example $collection = collect([ ['name' => 'John', 'age' => 30], ['name' => 'Jane', 'age' => 25], ['name' => 'Jack', 'age' => 40], ]); $sortKey = 'age'; // string $sortDirection = 'desc'; // string $sortedCollection = Sort::collection($collection, $sortKey, $sortDirection); // Sort direction is optional, it will use the default sort direction from the config file if not provided(by default it is desc) // output: [ ['name' => 'John', 'age' => 40], ['name' => 'Jane', 'age' => 30], ['name' => 'Jack', 'age' => 25], ] // eloquent example // This is useful when you have encrypted fields in your model. Querying the model will decrypt the fields, // then you can sort the collection using Sort::collection() method. // Sort direction is optional, it will use the default sort direction from the config file if not provided(by default it is desc) $query = User::query() ->select('name', 'age') ->get(); $sortKey = 'age'; // string $sortDirection = 'asc'; // string $sortedCollection = Sort::collection($collection, $sortKey, $sortDirection); //output: [ ['name' => 'Jack', 'age' => 25], ['name' => 'Jane', 'age' => 30], ['name' => 'John', 'age' => 40], ]
You can also retrieve the default sort direction from the config file. use CreativeCrafts\SortCollection\Sort; Sort::getDefaultSortDirection()
测试
composer test
更新日志
请参阅更新日志获取最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全漏洞
请查看我们的安全策略了解如何报告安全漏洞。
鸣谢
许可协议
MIT许可协议(MIT)。请参阅许可文件获取更多信息。