PHPackages                             laraditz/model-filter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. laraditz/model-filter

ActiveLaravel-package[Database &amp; ORM](/categories/database)

laraditz/model-filter
=====================

A simple eloquent model filter.

2.0.1(2mo ago)03801MITPHPPHP ^8.1

Since Apr 1Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/laraditz/model-filter)[ Packagist](https://packagist.org/packages/laraditz/model-filter)[ RSS](/packages/laraditz-model-filter/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (7)Versions (10)Used By (1)

Laravel Model Filter
====================

[](#laravel-model-filter)

[![Latest Stable Version](https://camo.githubusercontent.com/6164852f963642ffcdaafd97e683abc4e58aa5ababec3184586d57838aeb5cb2/68747470733a2f2f706f7365722e707567782e6f72672f6c6172616469747a2f6d6f64656c2d66696c7465722f762f737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/laraditz/model-filter)[![Total Downloads](https://camo.githubusercontent.com/456d48a87654a2be7af5d5a3217a1f1677dc355a3ecf9a022c0fd8c43df37acb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6172616469747a2f6d6f64656c2d66696c7465723f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laraditz/model-filter)[![License](https://camo.githubusercontent.com/7166eed2af92891a0acbbcd3e7483d54c3180fb7f521634e1b862ca05c6b0438/68747470733a2f2f706f7365722e707567782e6f72672f6c6172616469747a2f6d6f64656c2d66696c7465722f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/laraditz/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
------------

[](#requirements)

- PHP 8.1+
- Laravel 9 - 13

Installation
------------

[](#installation)

```
composer require laraditz/model-filter
```

Setup
-----

[](#setup)

Add the `Filterable` trait to your model and declare a `$filterable` allowlist:

```
use Laraditz\ModelFilter\Filterable;

class User extends Model
{
    use Filterable;

    protected array $filterable = [
        'name',
        'email',
        'age',
        'role.name',  // dot-notation opts in to relationship filtering
    ];

    public function role(): BelongsTo
    {
        return $this->belongsTo(Role::class);
    }
}
```

Optionally scaffold a filter class with the Artisan command:

```
php artisan make:filter UserFilter
```

This creates `app/Filters/UserFilter.php`. Use `--force` to overwrite an existing file.

Then add your custom methods:

```
namespace App\Filters;

use Laraditz\ModelFilter\Filter;

class UserFilter extends Filter
{
    public function name(mixed $value): void
    {
        $this->query->where('name', 'LIKE', $value . '%');
    }
}
```

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]=18` will call `age('18')` and silently drop the `gte` operator. 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
-----

[](#usage)

Pass `$request->all()` to `filter()`:

```
$users = User::filter($request->all())->get();
```

### Query param format

[](#query-param-format)

```
# Shorthand (defaults to eq)
?filters[name]=farhan

# Explicit operator
?filters[age][gte]=18

# Multiple filters - all AND'd together
?filters[status]=active&filters[age][gte]=18

# Multiple operators on same field
?filters[age][gte]=18&filters[age][lte]=65

# OR group - produces: AND (name LIKE '%far%' OR email LIKE '%far%')
?filters[or][name][like]=far&filters[or][email][like]=far

# Relationship filtering
?filters[role.name][eq]=admin

# Sort - top-level, not inside filters[]
?sort=name,-created_at

```

### Supported operators

[](#supported-operators)

OperatorSQL equivalentNotes`eq``= ?`Default when no operator bracket used`neq``!= ?``like``LIKE ?`Value wrapped in `%…%` automatically`gt``> ?``gte``>= ?``lt``< ?``lte``query->whereHas('role', function ($q): void {
            $q->where('name', 'admin')
              ->where('active', true);
        });
    }
}
```

Remember to add the key to `$filterable`:

```
protected array $filterable = ['active_admin'];
```

### Security

[](#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
------------------------

[](#breaking-changes-from-v1)

Areav1v2Query params`?name=farhan``?filters[name]=farhan`Laravel support7–129–13PHP minimum7.48.1LumenSupportedDropped`$filterable` emptyAll params passed throughAll filtering disabled`Filter::__call__`Auto-proxied to BuilderRemoved — use `$this->query` directly`Filter::sort()`On Filter base classMoved to Filterable traitCredits
-------

[](#credits)

- [Raditz Farhan](https://github.com/raditzfarhan)

License
-------

[](#license)

MIT. Please see the [license file](LICENSE) for more information.

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance86

Actively maintained with recent releases

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~262 days

Recently: every ~273 days

Total

8

Last Release

74d ago

Major Versions

1.0.5 → 2.0.02026-04-10

PHP version history (4 changes)1.0.0PHP ^7.2.5

1.0.2PHP ^7.2.5|^8.0

1.0.3PHP ^7.4|^8.0

2.0.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1203676?v=4)[Raditz Farhan](/maintainers/raditzfarhan)[@raditzfarhan](https://github.com/raditzfarhan)

---

Top Contributors

[![raditzfarhan](https://avatars.githubusercontent.com/u/1203676?v=4)](https://github.com/raditzfarhan "raditzfarhan (41 commits)")

---

Tags

laravelmodeleloquentfilter

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/laraditz-model-filter/health.svg)

```
[![Health](https://phpackages.com/badges/laraditz-model-filter/health.svg)](https://phpackages.com/packages/laraditz-model-filter)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.0M87](/packages/mongodb-laravel-mongodb)[prettus/l5-repository

Laravel 8|9|10|11|12|13 - Repositories to the database layer

4.2k11.2M153](/packages/prettus-l5-repository)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k29.9M42](/packages/kirschbaum-development-eloquent-power-joins)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k5.0M31](/packages/tucker-eric-eloquentfilter)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591452.8k2](/packages/spiritix-lada-cache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
