vati / filtero
一个Laravel包,用于从模型及其关系中过滤、搜索和排序数据
Requires
- php: ^7.4|^8.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
- nesbot/carbon: ^2.0
README
Filtero是一个Laravel包,提供了一种方便的方法来过滤、搜索和排序模型及其关系中的数据。您只需正确提供请求查询参数,Filtero就会为您处理剩余的工作。
安装
您可以通过composer安装此包
composer require vati/filtero
用法
-
将FilterTrait包含到您的模型中:使用提供的
FilterTrait
特性来启用过滤、搜索和排序功能。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' ]; }
-
在仓库或控制器中使用:在您的仓库或控制器中利用过滤、搜索和排序功能。
$payments = YourModel::with(['recipient'])->search()->filter()->sort()->paginate($request->per_page ?? 10);
示例查询
在您的仓库或控制器中的示例查询可能如下所示
$payments = YourModel::with(['recipient']) ->search() ->filter() ->sort() ->paginate($request->per_page ?? 10);
如果您只想执行搜索而不进行过滤或排序,您可以使用$payment->search()
。
API端点实际示例
以下是一个如何在API端点中使用Filtero的实际示例
/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
请求选项
您的请求可以包含各种过滤、搜索和排序的选项
-
搜索:使用
search
查询参数执行搜索。例如:?search=example@example.com
。 -
- 要搜索多个列,如搜索“John Doe”跨first_name和last_name,请在可搜索数组中使用CONCAT_WS函数。
-
排序:使用
sort
查询参数指定排序列。您可以使用点符号进行关系排序。例如:?sort=recipient.first_name
。 -
-
按降序排序属性:
?sort=-created_at
-
假设您想按
estimated_provider_fee
和estimated_platform_fee
的总和进行排序,按升序排序。您的请求URL将如下所示GET /your/route?sort=estimated_provider_fee{sum}estimated_platform_fee
-
-
- 这将返回基于
estimated_provider_fee
和estimated_platform_fee
总和排序的结果。
- 这将返回基于
-
- 关于总和的示例查询:
?sort=estimated_provider_fee{sum}estimated_platform_fee
- 关于总和的示例查询:
-
过滤:使用查询参数根据特定属性过滤数据。
-
- 例如
-
-
- 按相关
recipient
表中的city
属性过滤:?recipient[city]=New York
- 按相关
-
-
-
- 按主表中的
status
属性过滤:?status=completed
- 按主表中的
-
-
范围:您可以使用
range
查询参数指定日期属性的值范围。例如:?range[created_at][min]=2024-05-22&range[created_at][max]=2024-06-22
。
当然!以下是关于范围过滤的更新部分
范围过滤
范围过滤器允许您为数值属性指定一个范围,除了日期属性。此功能提供了基于各种数值标准的过滤数据的灵活性。
用法
要使用范围过滤
- 指定要过滤的属性范围的
min
和/或max
值。 - 在请求中使用
range
查询参数包含范围。
示例
// Example request URL ?range[price][min]=10&range[price][max]=100
在这个例子中,price
是一个数值属性,查询过滤价格在10到100之间的记录。
如果您有任何其他调整或补充,请随时告诉我!
配置
您可以通过修改config/filtero.php
文件来配置Filtero。配置选项包括
search_key
:搜索查询使用的关键字。sort_key
:排序查询使用的关键字。range_key
:范围查询使用的关键字。include_equal_in_range_filter
:选项用于在范围过滤中包含等于的值。
发布配置
要发布配置文件,请运行以下 Artisan 命令
php artisan vendor:publish --provider="Vati\Filtero\FilteroServiceProvider"
致谢
许可证
Filtero 包是开源软件,采用MIT 许可协议授权。