creativecrafts/laravel-sort-collection
Composer 安装命令:
composer require creativecrafts/laravel-sort-collection
包简介
A simple package to sort query collections.
README 文档
README
A simple handy package to sort query collections.
Installation
You can install the package via composer:
composer require creativecrafts/laravel-sort-collection
You can publish the config file with:
php artisan vendor:publish --tag="sort-collection-config"
This is the contents of the published config file:
return [ /** * The default sort direction to use when sorting a collection. * supported: asc, desc */ 'sort_direction' => 'desc', ];
Optionally, you can publish the views using
Usage
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()
Newly added methods
For a single encrypted column:
use CreativeCrafts\SortCollection\Sort; // In your controller or service $users = Sort::encryptedColumn(User::query(), 'encrypted_email', 'asc');
For multiple columns (some might be encrypted):
use CreativeCrafts\SortCollection\Sort; // Get all users first $users = User::all(); // Sort by multiple columns (last sort has the highest priority) $sortedUsers = Sort::multipleColumns($users, [ 'name' => 'asc', 'encrypted_email' => 'desc', 'created_at' => 'desc' ]);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
creativecrafts/laravel-sort-collection 适用场景与选型建议
creativecrafts/laravel-sort-collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 533 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「creativeCrafts」 「laravel-sort-collection」 「sort collection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 creativecrafts/laravel-sort-collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 creativecrafts/laravel-sort-collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 creativecrafts/laravel-sort-collection 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This is a handy package the can send email via AWS SES
A simple package that will provide a fluent api to interact with third party authentication providers
A handy package to access and interact with OpenAi endpoint
Laravel API Response is a powerful and flexible package that provides a standardized way to structure API responses in Laravel applications. It offers a range of features to enhance your API development experience, including consistent response formatting, conditional responses, pagination support,
A handy package to paginate laravel collection
A simple package to send sms messages using twillio
统计信息
- 总下载量: 533
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-19