Download the PHP package laraditz/model-filter without Composer
On this page you can find all versions of the php package laraditz/model-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package model-filter
Laravel Model Filter
A flexible Eloquent model filter for Laravel with operator support, OR grouping, and relationship filtering.
v2 breaking change: Filter params are now namespaced under
filters[]. See the breaking changes section below.
Requirements
- PHP 8.1+
- Laravel 9 - 13
Installation
Setup
Add the Filterable trait to your model and declare a $filterable allowlist:
Optionally scaffold a filter class with the Artisan command:
This creates app/Filters/UserFilter.php. Use --force to overwrite an existing file.
Then add your custom methods:
Fields without a custom method are handled automatically.
Custom method precedence: When a custom method exists for a field, it always takes precedence over auto-handling — and it receives only the value, not the operator. This means
?filters[age][gte]=18will callage('18')and silently drop thegteoperator. If you need operator-based filtering for a field, do not define a custom method for it and let auto-handling do the work.
Usage
Pass $request->all() to filter():
Query param format
Supported operators
| Operator | SQL equivalent | Notes |
|---|---|---|
eq |
= ? |
Default when no operator bracket used |
neq |
!= ? |
|
like |
LIKE ? |
Value wrapped in %…% automatically |
gt |
> ? |
|
gte |
>= ? |
|
lt |
< ? |
|
lte |
<= ? |
|
in |
IN (?) |
Comma-separated string → array |
between |
BETWEEN ? AND ? |
Comma-separated, exactly 2 values |
Relationship filtering
Add the relationship column in dot-notation to $filterable:
The package translates role.name into a whereHas('role', ...) clause automatically. All operators work:
Only one level of nesting is supported (relation.column). Deeper nesting (e.g. post.comments.body) is not supported.
Custom relationship query
If you need more control — for example filtering on a condition that spans multiple columns of the related model — define a custom method in your filter class:
Remember to add the key to $filterable:
Security
Only fields listed in $filterable can be filtered. Unlisted fields are silently ignored. Values are bound via PDO prepared statements - no SQL injection risk.
Breaking Changes from v1
| Area | v1 | v2 |
|---|---|---|
| Query params | ?name=farhan |
?filters[name]=farhan |
| Laravel support | 7–12 | 9–13 |
| PHP minimum | 7.4 | 8.1 |
| Lumen | Supported | Dropped |
$filterable empty |
All params passed through | All filtering disabled |
Filter::__call__ |
Auto-proxied to Builder | Removed — use $this->query directly |
Filter::sort() |
On Filter base class | Moved to Filterable trait |
Credits
License
MIT. Please see the license file for more information.
All versions of model-filter with dependencies
illuminate/support Version ^9.0|^10.0|^11.0|^12.0|^13.0
illuminate/database Version ^9.0|^10.0|^11.0|^12.0|^13.0