7amoood/eloquent-filter
Composer 安装命令:
composer require 7amoood/eloquent-filter
包简介
A Laravel package providing a powerful Eloquent trait for filtering, sorting, searching, and range queries via request parameters.
README 文档
README
A Laravel package that provides an Eloquent trait for filtering, sorting, searching, and range queries driven entirely by request parameters.
Requirements
- PHP 8.2+
- Laravel 11 or 12
Installation
composer require 7amoood/eloquent-filter
The service provider is auto-discovered. To publish the config file:
php artisan vendor:publish --tag=eloquent-filter
Quick Start
Add the HasFilter trait to your model and define which columns are filterable:
use Hamoood\EloquentFilter\HasFilter; class Post extends Model { use HasFilter; protected array $sortCols = ['created_at', 'title']; protected array $filterSearchCols = ['title', 'body']; protected array $filterCols = ['status', 'category_id']; protected int $filterLimit = 50; }
Then call the filter() scope in your controller:
$posts = Post::query()->filter()->paginate();
All filtering is now controlled via query string parameters.
Filter Types
Sorting
Sort results by an allowed column.
GET /posts?filter[sort][key]=created_at&filter[sort][type]=desc
keymust be listed in the model's$sortColsarray.typeisascordesc(defaults to the value in config).
Search
Search across multiple columns with a single term:
GET /posts?filter[search]=laravel
Or search specific fields:
GET /posts?filter[search][title]=laravel&filter[search][body]=eloquent
Searchable columns are defined in $filterSearchCols.
Column Filters
Filter by exact column values (comma-separated for multiple):
GET /posts?filter[column][status]=published
GET /posts?filter[column][category_id]=1,2,3
Passing an empty value filters for NULL:
GET /posts?filter[column][deleted_at]=
Allowed columns are defined in $filterCols.
Nested Relationship Filters
Filter through relationships using $filterColsChilds:
protected array $filterColsChilds = [ 'tags' => ['slug', 'name'], ];
GET /posts?filter[column][tags][slug]=php,laravel
Fetch
Fetch specific records by ID (or another column), bypassing any limit:
GET /posts?filter[fetch][id]=1,5,12
The $filterFetch property controls allowed columns (defaults to ['id']).
Column Range
Filter by a value range on any column (numeric, string, etc.). Supports providing both from and to, or just one of them:
GET /products?filter[range][column][price][from]=100&filter[range][column][price][to]=500
GET /products?filter[range][column][quantity][from]=10
GET /products?filter[range][column][rating][to]=5
Allowed columns are defined in $filterRangeColumn.
Date Range
Filter by a date range:
GET /posts?filter[range][date][created_at][from]=2025-01-01&filter[range][date][created_at][to]=2025-12-31
Allowed columns are defined in $filterRangeDate.
Time Range
Filter by a time range (handles overnight spans like 22:00 to 06:00):
GET /posts?filter[range][time][starts_at][from]=09:00&filter[range][time][starts_at][to]=17:00
Allowed columns are defined in $filterRangeTime.
Limit
Override the per-page count (capped by $filterLimit or the config default):
GET /posts?filter[limit]=25
Model Properties Reference
| Property | Type | Description |
|---|---|---|
$sortCols |
array |
Columns allowed for sorting |
$filterSearchCols |
array |
Columns included in search queries |
$filterCols |
array |
Columns allowed for exact-match filtering |
$filterColsChilds |
array |
Relationship columns for nested filtering |
$filterFetch |
array |
Columns allowed for fetch (default: ['id']) |
$filterRangeColumn |
array |
Columns allowed for generic value range filtering |
$filterRangeDate |
array |
Columns allowed for date range filtering |
$filterRangeTime |
array |
Columns allowed for time range filtering |
$filterLimit |
int |
Max per-page limit for this model |
Column Aliasing
All column arrays support aliasing via key-value pairs. The key is the public name used in the request, and the value is the actual database column:
protected array $filterCols = [ 'type' => 'category_id', // ?filter[column][type]=5 queries category_id 'status', // ?filter[column][status]=active queries status ];
Configuration
After publishing, the config file is at config/eloquent-filter.php:
return [ // Request parameter key containing filter data 'request_key' => 'filter', // Max rows when model has no $filterLimit (null to disable) 'default_limit' => 1000, // Default sort direction 'default_sort_direction' => 'asc', ];
Custom Request Key
You can override the request key per-call:
Post::query()->filter(key: 'f')->paginate(); // reads from ?f[search]=...
Dynamic Sort Columns
Add sort columns at query time with the sortCols scope:
Post::query()->sortCols(['views', 'likes'])->filter()->paginate();
License
MIT
7amoood/eloquent-filter 适用场景与选型建议
7amoood/eloquent-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「filter」 「query」 「sort」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 7amoood/eloquent-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 7amoood/eloquent-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 7amoood/eloquent-filter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Nova searchable filter for belongsTo relationships.
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Query filtering in your frontend
Abstraction Layer to index and search entities
Anax Database Active Record module for model classes.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 46
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-11