PHPackages                             oooiik/laravel-query-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. oooiik/laravel-query-filter

ActiveLibrary[Database &amp; ORM](/categories/database)

oooiik/laravel-query-filter
===========================

A clean, convention-based way to extract Eloquent query filters into dedicated filter classes — keep your controllers and scopes thin.

1.3.0(1mo ago)01.1k↓80.6%2[4 issues](https://github.com/oooiik/laravel-query-filter/issues)MITPHPPHP ^7.3|^8.0

Since Oct 27Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/oooiik/laravel-query-filter)[ Packagist](https://packagist.org/packages/oooiik/laravel-query-filter)[ Docs](https://github.com/oooiik/laravel-query-filter)[ RSS](/packages/oooiik-laravel-query-filter/feed)WikiDiscussions master Synced today

READMEChangelog (8)Dependencies (3)Versions (10)Used By (0)

Laravel Query Filter
====================

[](#laravel-query-filter)

[![Latest Version on Packagist](https://camo.githubusercontent.com/86175b1c2b223c1f7b28e1cfb0482a5684b55f6ac6a97a484c8b1a1b87bf5b96/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6f6f69696b2f6c61726176656c2d71756572792d66696c7465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/oooiik/laravel-query-filter)[![Total Downloads](https://camo.githubusercontent.com/7c13a749c4a62bb94313907eaeba20fca54ea1c25ce48a924086d8600f69baaa/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f6f6f69696b2f6c61726176656c2d71756572792d66696c7465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/oooiik/laravel-query-filter)[![PHP Version](https://camo.githubusercontent.com/191a3d7a65301ae4d1ae556cb6c46aaa402acde8c82329d8fd68412bd8272dcf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6f6f6f69696b2f6c61726176656c2d71756572792d66696c7465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/oooiik/laravel-query-filter)[![License](https://camo.githubusercontent.com/da53739ab13288f6145ec0589cc44ebc7e9eca5a1dd28f664a08e9ef711f73e8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f6f6f69696b2f6c61726176656c2d71756572792d66696c7465722e7376673f7374796c653d666c61742d737175617265)](LICENSE)

A clean, convention-based way to extract Eloquent query filters into dedicated filter classes. Keep your controllers thin, your scopes focused, and your filtering logic testable.

```
// Before — filtering logic leaking into the controller
$users = User::query()
    ->when($request->username, fn($q, $v) => $q->where('username', $v))
    ->when($request->role, fn($q, $v) => $q->whereHas('role', fn($r) => $r->where('title', $v)))
    ->when($request->created_after, fn($q, $v) => $q->where('created_at', '>=', $v))
    ->paginate();

// After — one line, all filtering in UserFilter
$users = User::filter($request->validated())->paginate();
```

Features
--------

[](#features)

- 🎯 **Convention over configuration** — each public method on your filter class becomes a filter key. No registration, no metadata.
- 🪶 **Single trait + base class** — add `Filterable` to a model, point it at a filter class, done.
- 🛠 **Artisan generator** — `php artisan make:filter UserFilter` scaffolds the class for you.
- 🔁 **Composable** — apply multiple parameter sets to the same filter instance and chain into the query.
- ⚙️ **Defaults &amp; fallbacks** — provide default parameter values and fallback handlers for missing keys.
- 🧩 **Laravel 6 → 12** — broad compatibility, PHP 7.3+ through 8.x.

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

[](#installation)

```
composer require oooiik/laravel-query-filter
```

The service provider is auto-registered via Laravel's package discovery.

Quick Start
-----------

[](#quick-start)

### 1. Generate a filter

[](#1-generate-a-filter)

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

This creates `app/Filters/UserFilter.php`.

### 2. Define your filter methods

[](#2-define-your-filter-methods)

Each public method becomes a filter key matching its name:

```
namespace App\Filters;

use Oooiik\LaravelQueryFilter\Filters\QueryFilter;

class UserFilter extends QueryFilter
{
    public function username($username)
    {
        $this->builder->where('username', $username);
    }

    public function role($role)
    {
        $this->builder->whereHas('role', function ($query) use ($role) {
            $query->where('title', $role);
        });
    }

    public function createdAfter($date)
    {
        $this->builder->where('created_at', '>=', $date);
    }
}
```

### 3. Attach the filter to your model

[](#3-attach-the-filter-to-your-model)

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Oooiik\LaravelQueryFilter\Traits\Model\Filterable;
use App\Filters\UserFilter;

class User extends Model
{
    use Filterable;

    protected $defaultFilter = UserFilter::class;
}
```

### 4. Use it

[](#4-use-it)

```
// Controller
public function index(Request $request)
{
    $validated = $request->validate([
        'username'      => 'nullable|string',
        'role'          => 'nullable|string',
        'createdAfter'  => 'nullable|date',
    ]);

    return User::filter($validated)->paginate();
}
```

Missing keys are silently ignored — only the filter methods that match input parameters run.

Advanced Usage
--------------

[](#advanced-usage)

### Default parameters

[](#default-parameters)

Use the `$default` property to pre-fill values when a key is missing from the input:

```
class UserFilter extends QueryFilter
{
    public $default = [
        'status' => 'active',
        'sort'   => 'created_at',
    ];

    public function status($status)
    {
        $this->builder->where('status', $status);
    }

    public function sort($column)
    {
        $this->builder->orderBy($column, 'desc');
    }
}
```

Calling `User::filter([])` will still apply `status = active` and `sort by created_at desc`.

### Fallback methods

[](#fallback-methods)

Use `$fallback` to redirect missing input keys to a different method:

```
class UserFilter extends QueryFilter
{
    public $fallback = [
        'search' => 'searchByName',
    ];

    public function searchByName($value)
    {
        $this->builder->where('name', 'like', "%{$value}%");
    }
}
```

If the `search` key is missing from input, `searchByName` runs with whatever value was provided as the fallback source.

### Standalone filter instance (chaining)

[](#standalone-filter-instance-chaining)

Apply multiple parameter sets to the same filter:

```
$filter = User::createFilter(UserFilter::class);

$filter->apply(['role' => 'admin']);
$filter->apply(['status' => 'active']);

$query = $filter->query();
// Both filter sets are now applied to the builder
```

### Accessing all parameters

[](#accessing-all-parameters)

Filter methods receive the full parameter array as a second argument:

```
public function username($username, $allParams)
{
    if (! empty($allParams['exact_match'])) {
        $this->builder->where('username', $username);
    } else {
        $this->builder->where('username', 'like', "%{$username}%");
    }
}
```

Comparison with `spatie/laravel-query-builder`
----------------------------------------------

[](#comparison-with-spatielaravel-query-builder)

`laravel-query-filter``spatie/laravel-query-builder`**Approach**Convention-based — method = filter keyDeclarative — register allowed filters explicitly**Per-model class**Yes, dedicated filter classOptional, often inline**Custom filter logic**Plain PHP method`AllowedFilter::callback()`**Best for**Complex filtering with reusable logicAPI endpoints with simple filtering needsBoth are great — choose `laravel-query-filter` when you want a dedicated class per model with reusable, testable filter logic.

Requirements
------------

[](#requirements)

- PHP 7.3 or higher
- Laravel 6.x — 12.x

Compatibility Matrix
--------------------

[](#compatibility-matrix)

LaravelPHPStatus12.x8.2+✅ Supported11.x8.2+✅ Supported10.x8.1+✅ Supported9.x8.0+✅ Supported8.x7.3+✅ Supported7.x7.3+✅ Supported6.x7.3+✅ SupportedContributing
------------

[](#contributing)

Pull requests are welcome. For substantial changes, please open an issue first to discuss the direction.

Bug reports and feature ideas → [GitHub Issues](https://github.com/oooiik/laravel-query-filter/issues).

Credits
-------

[](#credits)

- [Obidjon Toshev](https://oooiik.com) — author &amp; maintainer
- All [contributors](https://github.com/oooiik/laravel-query-filter/graphs/contributors)

License
-------

[](#license)

The MIT License (MIT). See [LICENSE](LICENSE) for details.

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance93

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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 ~187 days

Recently: every ~324 days

Total

8

Last Release

34d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f56b318fe17172a3b59e47d2e378a4c415fecc96ed642024d3a2a51a0356220?d=identicon)[oooiik](/maintainers/oooiik)

---

Top Contributors

[![oooiik](https://avatars.githubusercontent.com/u/77920516?v=4)](https://github.com/oooiik "oooiik (7 commits)")

---

Tags

searchlaraveleloquentqueryfilterquery builderscopequery-filter

### Embed Badge

![Health badge](/badges/oooiik-laravel-query-filter/health.svg)

```
[![Health](https://phpackages.com/badges/oooiik-laravel-query-filter/health.svg)](https://phpackages.com/packages/oooiik-laravel-query-filter)
```

###  Alternatives

[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k43.2M631](/packages/spatie-laravel-medialibrary)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k5.1M34](/packages/tucker-eric-eloquentfilter)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[mehdi-fathi/eloquent-filter

Eloquent Filter adds custom filters automatically to your Eloquent Models in Laravel.It's easy to use and fully dynamic, just with sending the Query Strings to it.

448199.3k1](/packages/mehdi-fathi-eloquent-filter)[itpathsolutions/dbstan

Database Standardization and Analysis Tool for Laravel

492.8k](/packages/itpathsolutions-dbstan)

PHPackages © 2026

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