pedrokeilerbatistarojo/smartfilter
Composer 安装命令:
composer require pedrokeilerbatistarojo/smartfilter
包简介
This Laravel package provides a streamlined solution for filtering and listing data. It accepts an input array, applies customizable filters, and returns a structured JSON response. Ideal for projects requiring dynamic data filtering with minimal setup, the package ensures consistency in API respons
README 文档
README
SmartFilter is a Laravel package designed to provide robust and flexible filtering capabilities for APIs. It allows developers to apply filters, sorting, and pagination to data retrieval seamlessly while returning structured JSON responses.
Installation
-
Require the package via Composer:
composer require pedrokeilerbatistarojo/smartfilter
-
Publish the configuration (optional):
php artisan vendor:publish --provider="Pedrokeilerbatistarojo\Smartfilter\SmartfilterServiceProvider" -
Usage: Include the package in your controller or service to start applying filters.
Features
- Apply filters with various operators (e.g.,
like,=, etc.). - Select specific columns to retrieve.
- Include relationships for eager loading.
- Sort results by any field.
- Paginate results with customizable parameters.
- Structured JSON responses with metadata.
Usage Example
Sample Request
GET /api/users?filters[0][0]=name&filters[0][1]=like&filters[0][2]=Owner&columns[]=id&columns[]=name&columns[]=email&includes[]=role&sortField=created_at&sortType=asc&itemsPerPage=8¤tPage=1
Query Params
filters:[["name", "like", "Owner", "and"],["email", "like","owner@example.com", "and"],["name", "like", "Owner", "and", "role"]]
columns:["id", "name", "email"]
includes:["role"]
sortField:created_at
sortType:asc
itemsPerPage:8
currentPage:1
Controller Example
use Pedrokeilerbatistarojo\Smartfilter\Services\FilterService; use Pedrokeilerbatistarojo\Smartfilter\Helpers\ResponseHelper; use Illuminate\Http\Request; use App\Models\User; use Exception; class UserController extends Controller { public function __construct( private readonly FilterService $filterService ){ } /** * @throws Exception */ public function __invoke(Request $request) { try { $response = $this->filterService->execute(User::class, $request->all()); return ResponseHelper::sendResponse($response); } catch(Exception $ex){ return ResponseHelper::sendError($ex->getMessage()); } } }
Expected JSON Response
{
"success": true,
"message": "Search completed successfully",
"errors": null,
"payload": {
"items": [
{
"id": 1,
"name": "Owner",
"email": "owner@example.com"
},
{
"id": 2,
"name": "Admin",
"email": "admin@example.com"
}
],
"metadata": {
"currentPage": 1,
"lastPage": 5,
"itemsPerPage": 8,
"total": 40
},
"total": 40
}
}
Testing
-
Run the test suite:
php artisan test -
Example Test:
public function test_filter_with_filters(): void { $filters = [ ['name', 'like', 'Owner', 'and'], ['email', 'like', 'owner@example.com', 'and'], ['name', 'like', 'Owner', 'and', 'role'] ]; $columns = ['id', 'name', 'email']; $includes = ['role']; $params = [ 'filters' => $filters, 'columns' => $columns, 'includes' => $includes, 'sortField' => 'created_at', 'sortType' => 'asc', 'itemsPerPage' => 8, 'currentPage' => 1 ]; $queryString = http_build_query($params); $endpoint = "/api/users?{$queryString}"; $response = $this->get($endpoint); $response->assertStatus(200); $response->assertJsonStructure([ 'success', 'message', 'errors', 'payload' => [ 'items' => [ '*' => [ 'id', 'name', 'email' ] ], 'metadata' => [ 'currentPage', 'lastPage', 'itemsPerPage', 'total' ], 'total' ] ]); }
Contributing
Feel free to fork this repository and submit pull requests. Ensure that all tests pass and maintain code quality standards.
License
SmartFilter is open-source software licensed under the MIT License.
pedrokeilerbatistarojo/smartfilter 适用场景与选型建议
pedrokeilerbatistarojo/smartfilter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pedrokeilerbatistarojo/smartfilter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pedrokeilerbatistarojo/smartfilter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-05