san4io / eloquent-filter
Laravel Eloquent 过滤器包
0.2
2017-11-01 21:01 UTC
Requires
- php: >=7.1
- illuminate/database: 5.*
- illuminate/support: 5.*
Requires (Dev)
- orchestra/testbench: ~3.0
- phpunit/phpunit: ^6.4
This package is not auto-updated.
Last update: 2024-09-29 02:49:48 UTC
README
一个简单的、优雅的Eloquent模型过滤器包。
要求
- "php": ">=7.1",
- "illuminate/support": "5.*",
- "illuminate/database": "5.*"
安装
composer require san4io/eloquent-filter
用法
模型
将 Filterable 特性添加到你的Eloquent模型。
创建 $filterable 属性,并使用与模型属性过滤相对应的映射。
class Event extends Model { use Filterable; protected $fillable = [ 'country_id', 'title', ]; protected $filterable = [ 'country_id' => WhereFilter::class, 'title' => LikeFilter::class, 'created_at' => BetweenFilter::class ]; }
控制器
只需将请求参数作为数组传递给过滤器。
public function index(Request $request): JsonResponse { $events = Event::filter($request->all())->paginate(); // or get(), first() or whatever return response()->json($events); }
请求示例
https:///api/v1/events?country_id=101&title=Sport
https:///api/v1/events?country_id[]=101&country_id[]=102&title=Sport
https:///api/v1/events?created_at[from]=2017-07-11&created_at[till]=2017-08-11&title=Sport
过滤器
此包旨在易于扩展,因此如果您需要一些自定义过滤器,可以扩展AbstractFilter并在模型中添加必要的映射。
默认情况下有3种过滤器类型
- WhereFilter
- LikeFilter
- BetweenFilter
WhereFilter
接受整数或整数数组
LikeFilter
接受字符串
BetweenFilter
接受包含键['from']和['till']的数组
路线图
- 测试
- 过滤关系
贡献
欢迎任何贡献!