kamil-koscielniak/eloquent-filters
Composer 安装命令:
composer require kamil-koscielniak/eloquent-filters
包简介
Simply way to add filters to Eloquent models
README 文档
README
Laravel package for simply adding filters to Eloquent models.
Instalation
composer require kamil-koscielniak/eloquent-filters
php artisan vendor:publish --tag=config
Usage
Step 1 - Define filters in Eloquent model
<?php namespace App; use Illuminate\Database\Eloquent\Model; use KamilKoscielniak\EloquentFilters\Filters\PartialFilter; use KamilKoscielniak\EloquentFilters\Filters\RangeFilter; use KamilKoscielniak\EloquentFilters\Traits\Filterable; class Product extends Model { use Filterable; public static array $filters = [ 'code' => PartialFilter::class, 'price' => RangeFilter::class, ]; }
Note that the names of filters (code and price in above example) must be same as the column names in database table.
Step 2 - Filtering data
For example you can filter your data like this
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Product; class ProductController extends Controller { public function index(Request $request) { $products = Product::filter($request)->get(); return response()->json(compact('products')); } }
Step 3 - Use filters in query string
Example url below will return products with price between 21.99 and 99.99
http://localhost/products?price=21.99/99.99
Available filter types
PartialFilter
For partial searching.
Example usage:
http://localhost/products?name=blue
Above example will retrieve products which names contains phrase blue
ExactFilter
For exact searching.
Example usage:
http://localhost/customers?name=mike
Above example will retrieve customers which names are equals to mike
RangeFilter
For range searching.
Use range separator / to separate min and max values
Example usage:
http://localhost/products?price=21.99/99.99
Above example will retrieve products which price are between 21.99 and 99.99
Note that provided values must be numeric.
By default RangeFilter use operators <= and >=.
If you don't want include provided values in search results than use exclusion mode.
Example usage with exclusion mode:
http://localhost/products?price=21.99|e/99.99
Above example will retrieve products which price is greater than 21.99 and lower or equal than 99.99
Searching in relationships
Sample eloquent model with relationship filter:
<?php namespace App; use Illuminate\Database\Eloquent\Model; use KamilKoscielniak\EloquentFilters\Filters\PartialFilter; use KamilKoscielniak\EloquentFilters\Traits\Filterable; class Product extends Model { use Filterable; public static array $filters = [ 'category__code' => PartialFilter::class, ]; public function category() { return $this->belongsTo(Category::class); } }
In example above filter category__code is relationship filter because it use FILTERS_RELATIONSHIP_SEPARATOR
in name (by default __). In this filter name category is name of relationship and code is column name in related Category model.
So this filter give you possibility to filter Product models by code of related Category model.
Exclusion mode
In each of filter types you can use exclusion mode. Just add |e suffix to value that you want to exclude.
If you want use custom suffix you can change it, see Configuration section below.
Configuration
Run php artisan vendor:publish --tag=config
Config file you will find in app/filters.php
Available options
| Option | Default value | Description |
|---|---|---|
| FILTERS_EXCLUSION_SUFFIX | |e | For exclusion value. Example usage ?name=bike|e |
| FILTERS_RANGE_SEPARATOR | / |
For range searching. Use in query string. Example usage ?price=21.99/99.99 |
| FILTERS_RELATIONSHIP_SEPARATOR | __ |
When you want to search in related object use relationship separator ?customer__name=mike |
License
The MIT License (MIT). Please see License File for more information.
kamil-koscielniak/eloquent-filters 适用场景与选型建议
kamil-koscielniak/eloquent-filters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 08 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「filters」 「eloquent」 「eloquent-filters」 「kamil-koscielniak」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kamil-koscielniak/eloquent-filters 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kamil-koscielniak/eloquent-filters 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kamil-koscielniak/eloquent-filters 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Simple filters for laravel
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
Livewire Filters is a series of Livewire components that provide you with the tools to do live filtering of your data from your own Livewire components.
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-30