ahmed-aliraqi/laravel-filterable
Composer 安装命令:
composer require ahmed-aliraqi/laravel-filterable
包简介
Elegant query filtering for Laravel Eloquent models
README 文档
README
A simple and elegant way to add filtering capabilities to your Laravel Eloquent models.
Installation
You can install the package via composer:
composer require ahmed-aliraqi/laravel-filterable
Usage
1. Create a Filter Class
Use the provided artisan command to create a new filter class:
php artisan make:filter UserFilter
This will create a new filter class in app/Http/Filters:
namespace App\Http\Filters; use AhmedAliraqi\LaravelFilterable\BaseFilter; class UserFilter extends BaseFilter { protected array $filters = [ 'name', 'email', 'age' ]; public function name($value) { $this->builder->where('name', 'like', "$value%"); } public function email($value) { $this->builder->where('email', $value); } public function age($value) { $this->builder->where('age', $value); } }
2. Apply the Trait to Your Model
Add the Filterable trait to your model:
use AhmedAliraqi\LaravelFilterable\Filterable; class User extends Model { use Filterable; // Optionally specify the filter class protected $filter = UserFilter::class; }
3. Use the Filter
You can now filter your model queries:
// In your controller use App\Http\Filters\UserFilter; public function index(Request $request) { $users = User::filter(new UserFilter($request->query()))->get(); return response()->json($users); }
Or if you've specified the filter class in your model:
$users = User::filter()->get();
Query Parameters
Your API endpoints will now automatically respond to query parameters that match your defined filters:
GET /api/users?name=John&age=25
Including Relationships
You can include relationships in your filtered results by defining supported includes in your filter class:
class UserFilter extends BaseFilter { protected array $filters = ['name', 'email']; protected array $supportedInclude = [ 'posts', 'comments', 'posts.comments' // Nested relationships ]; }
Then use the include parameter in your requests:
Include single relation
GET /api/users?include=posts
Include multiple relations (comma-separated)
GET /api/users?include=posts,comments
Include nested relations (dot notation)
GET /api/users?include=posts.comments
You can also load includes on an existing model instance:
$user = User::find(1); $user->loadIncludes(new UserFilter(['include' => 'posts,comments']));
Advanced Usage
Custom Namespaces
You can create filters in custom namespaces:
php artisan make:filter Filters/CustomFilter
Complex Filters
You can define complex filter methods:
class UserFilter extends BaseFilter { protected array $filters = [ 'age_range', 'created_between' ]; public function ageRange($value) { [$min, $max] = explode(',', $value); $this->builder->whereBetween('age', [$min, $max]); } public function createdBetween($value) { [$start, $end] = explode(',', $value); $this->builder->whereBetween('created_at', [$start, $end]); } }
Filter Arrays
You can handle array inputs in your filters:
class UserFilter extends BaseFilter { protected array $filters = ['status']; public function status(array $value) { $this->builder->whereIn('status', $value); } }
Testing
composer test
Authors
License
The MIT License (MIT). Please see License File for more information.
ahmed-aliraqi/laravel-filterable 适用场景与选型建议
ahmed-aliraqi/laravel-filterable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 528 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ahmed-aliraqi/laravel-filterable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ahmed-aliraqi/laravel-filterable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 528
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-24