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

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

kblais/query-filter
===================

Easily create filters for your Eloquent models.

v3.3.0(1y ago)2445.1k↓43.2%3[1 PRs](https://github.com/kblais/query-filter/pulls)1MITPHPPHP ^8.0CI passing

Since May 10Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/kblais/query-filter)[ Packagist](https://packagist.org/packages/kblais/query-filter)[ RSS](/packages/kblais-query-filter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (30)Used By (1)

QueryFilter [![Build Status](https://camo.githubusercontent.com/121508af71b0f0c7dbc95b937c74aa2e351f9e5d48f779ca48205446899e9053/68747470733a2f2f7472617669732d63692e6f72672f6b626c6169732f71756572792d66696c7465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kblais/query-filter)
====================================================================================================================================================================================================================================================================================================

[](#queryfilter-)

Easily create filters for your Eloquent model.

Based on [Jeffray Way's Laracast tutorial](https://github.com/laracasts/Dedicated-Query-String-Filtering/).

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

[](#installation)

You can install the package via composer:

```
composer require kblais/query-filter
```

You can publish the config file with:

```
php artisan vendor:publish --provider="Kblais\QueryFilter\QueryFilterServiceProvider" --tag="query-filter-config"
```

This is the contents of the published config file:

```
return [
    'default-filters-source' => null,
];
```

Usage
-----

[](#usage)

A QueryFilter is a class to apply, based on an array or a Request, multiple conditions.

You can call any Eloquent method directly from filter methods.

```
use Kblais\QueryFilter\QueryFilter;

class PostFilter extends QueryFilter
{
    public function title($value)
    {
        return $this->where('foo', 'bar');
    }

    public function author($value)
    {
        return $this->whereHas('author', function ($builder) use ($value) {
            $this->where('name', $value);
        });
    }
}
```

To allow a model to use query filters, you have to add the `Filterable` trait on your model.

```
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Kblais\QueryFilter\Filterable;

class Post extends Model
{
    use Filterable;
}
```

You can then use the `filter()` scope from anywhere:

```
// From an array...
$filterInput = [
    'title' => 'Les Trois Mousquetaires',
];

$posts = Post::filter(PostFilter::make($filterInput))->get();

// ...Or in a controller action
public function index(PostFilter $filter)
{
    // Filter is automatically populated with Request data when injected
    return Post::filter($filter)->get();
}
```

If your filter parameters are always placed in an array key (for example `filters`), you can define the `default-filters-source` config key in the config file, or add a `protected string $source = 'filters'` in your QueryFilter.

Frequent issues
---------------

[](#frequent-issues)

### `Call to an undefined method App\QueryFilters\YourFilter::anEloquentScope().` with PHPStan

[](#call-to-an-undefined-method-appqueryfiltersyourfilteraneloquentscope-with-phpstan)

To fix this error message, add the following DocBlock to your filter:

```
/**
 * @mixin \App\Models\ModelOfYourScope
 */

```

Testing
-------

[](#testing)

The test suite is composed of 3 tests: PHPCsFixer (coding style), PHPStan (static analysis) and PHPUnit (unit tests).

You can run all these tests running the following command:

```
composer tests

```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

- Follow the PSR-2 Coding Standard. Use PHP-CS-Fixer to apply the conventions.
- Add tests for the features you add and bugs you discover.

Credits
-------

[](#credits)

- [Killian BLAIS](https://github.com/kblais)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance69

Regular maintenance activity

Popularity38

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 80.6% 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 ~202 days

Recently: every ~343 days

Total

17

Last Release

426d ago

Major Versions

1.7.0 → v2.x-dev2021-01-25

v2.0.2 → v3.0.02022-02-15

PHP version history (5 changes)1.0.0PHP &gt;=5.4.0

1.1.0PHP &gt;=5.5.0

v1.5.0PHP &gt;=7.1

1.7.0PHP ^7.2|^8.0

v2.x-devPHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/97939cae40692a836e7ebb41802ae630abfb5d3b680e01cd9e41eb262c33e78d?d=identicon)[kblais](/maintainers/kblais)

---

Top Contributors

[![kblais](https://avatars.githubusercontent.com/u/1931954?v=4)](https://github.com/kblais "kblais (54 commits)")[![jgrossi](https://avatars.githubusercontent.com/u/1175275?v=4)](https://github.com/jgrossi "jgrossi (12 commits)")[![mingyoung](https://avatars.githubusercontent.com/u/6228858?v=4)](https://github.com/mingyoung "mingyoung (1 commits)")

---

Tags

eloquentlaravellaravel-package

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-query-builder

Easily build Eloquent queries from API requests

4.4k26.9M220](/packages/spatie-laravel-query-builder)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[jerome/filterable

Streamline dynamic Eloquent query filtering with seamless API request integration and advanced caching strategies.

19226.1k](/packages/jerome-filterable)[outerweb/settings

Application wide settings stored in your database

4899.2k5](/packages/outerweb-settings)[simplestats-io/laravel-client

Client for SimpleStats!

4515.5k](/packages/simplestats-io-laravel-client)

PHPackages © 2026

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