vati/filtero
Composer 安装命令:
composer require vati/filtero
包简介
A Laravel package to filter, search, sort data from models and their relations
README 文档
README
Filtero is a Laravel package that provides a convenient way to filter, search, and sort data from models and their relationships. You only have to provide request query params properly, and Filtero will handle the rest for you.
Installation
You can install the package via composer:
composer require vati/filtero
Usage
-
Include FilterTrait in your Model: Use the provided
FilterTraittrait in your model to enable filtering, searching, and sorting capabilities.use Vati\Filtero\FilterTrait; class YourModel extends Model { use FilterTrait; /** * The columns that are searchable. * * @var array */ protected array $searchable = [ 'status', [ 'recipient' => [ 'CONCAT_WS(" ", first_name, last_name)' ], 'currency' => [ 'code' ] ], ]; /** * The columns that are filterable. * * @var array */ protected array $filterable = [ 'status', 'currency_id', 'provider_transaction_id', [ 'recipient' => [ 'country_id', 'city', 'phone', 'email' ] ], ]; /** * The columns that are sortable. * * @var array */ protected array $sortable = [ 'id', 'user_id', 'recipient_id', 'recipient.first_name', //relation sort 'payout_id', 'estimated_provider_fee{sum}estimated_platform_fee', // sum multiple columns, order must be same in http_query 'payment_amount', 'total_amount', 'currency_id', 'status', 'reference', 'created_at' ]; }
-
Use in Repository or Controller: Utilize the filtering, searching, and sorting capabilities in your repository or controller.
$payments = YourModel::with(['recipient'])->search()->filter()->sort()->paginate($request->per_page ?? 10);
Example Query
An example query in your repository or controller would look like:
$payments = YourModel::with(['recipient']) ->search() ->filter() ->sort() ->paginate($request->per_page ?? 10);
If you only want to perform a search without filtering or sorting, you can use $payment->search().
Api Endpoint Realistic Example
Here's a realistic example of how you can use Filtero in your API endpoints:
/payment/payments?search=example@example.com&sort=recipient.first_name&status=completed¤cy_id=3¤cy[code]=CHF&range[created_at][min]=2024-05-22&range[created_at][max]=2024-06-22
Request Options
Your request can contain various options for filtering, searching, and sorting:
-
Search: Use the
searchquery parameter to perform a search. Example:?search=example@example.com. -
- To search multiple columns as one, like searching for "John Doe" across first_name and last_name, use the CONCAT_WS function in your searchable array.
-
Sort: Use the
sortquery parameter to specify the sorting column. You can use dot notation for relational sorting. Example:?sort=recipient.first_name. -
-
Sort by the attribute in descending order:
?sort=-created_at -
Let's say you want to sort the results based on the sum of
estimated_provider_feeandestimated_platform_fee, in ascending order. Your request URL would look like this:GET /your/route?sort=estimated_provider_fee{sum}estimated_platform_fee
-
-
- This will return the results sorted based on the sum of
estimated_provider_feeandestimated_platform_fee.
- This will return the results sorted based on the sum of
-
- Example query for sum:
?sort=estimated_provider_fee{sum}estimated_platform_fee
- Example query for sum:
-
Filter: Use query parameters to filter data based on specific attributes.
-
- For Example:
-
-
- To filter by the
cityattribute in the relatedrecipienttable:?recipient[city]=New York
- To filter by the
-
-
-
- To filter by the
statusattribute in the main table:?status=completed
- To filter by the
-
-
Range: You can specify a range for date attributes using the
rangequery parameter. Example:?range[created_at][min]=2024-05-22&range[created_at][max]=2024-06-22.
Of course! Here's the updated section regarding the range filter:
Range Filter
The range filter allows you to specify a range for numeric attributes, in addition to date attributes. This feature provides flexibility in filtering data based on various numeric criteria.
Usage
To use the range filter:
- Specify the
minand/ormaxvalues for the range of the attribute you want to filter. - Include the range in the request using the
rangequery parameter.
Example
// Example request URL ?range[price][min]=10&range[price][max]=100
In this example, price is a numeric attribute, and the query filters records where the price falls within the range of
10 to 100.
Feel free to let me know if there are any more adjustments or additions you'd like!
Configuration
You can configure Filtero by modifying the config/filtero.php file. The configuration options include:
search_key: The key used for search queries.sort_key: The key used for sorting queries.range_key: The key used for range queries.include_equal_in_range_filter: Option to include equal values in range filtering.
Publishing Configuration
To publish the configuration file, run the following Artisan command:
php artisan vendor:publish --provider="Vati\Filtero\FilteroServiceProvider"
Credits
License
The Filtero package is open-sourced software licensed under the MIT license.
vati/filtero 适用场景与选型建议
vati/filtero 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 vati/filtero 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vati/filtero 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-07