PHPackages                             grantholle/laravel-model-filters - 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. [Search &amp; Filtering](/categories/search)
4. /
5. grantholle/laravel-model-filters

ActiveLibrary[Search &amp; Filtering](/categories/search)

grantholle/laravel-model-filters
================================

A composable way to add filters to your model queries.

1.2.0(1y ago)020[3 PRs](https://github.com/grantholle/laravel-model-filters/pulls)MITPHPPHP ^8.2CI passing

Since Jan 25Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/grantholle/laravel-model-filters)[ Packagist](https://packagist.org/packages/grantholle/laravel-model-filters)[ Docs](https://github.com/grantholle/laravel-model-filters)[ GitHub Sponsors]()[ RSS](/packages/grantholle-laravel-model-filters/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (13)Versions (10)Used By (0)

Laravel Model Filters
=====================

[](#laravel-model-filters)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b436fc4714ae5fff486144ed558a2d76c45c773ccef0c29bf0a83ddae6d84b5e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6772616e74686f6c6c652f6c61726176656c2d6d6f64656c2d66696c746572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/grantholle/laravel-model-filters)[![GitHub Tests Action Status](https://camo.githubusercontent.com/1c4097363a018a7cbd6bdd50d7b9bb8d1653e2309b05a2ed9f639925a8dbba4f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6772616e74686f6c6c652f6c61726176656c2d6d6f64656c2d66696c746572732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/grantholle/laravel-model-filters/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/b1c75033c2547dc5c4d37e40865460aea594a8f6595c5e67898fb651067dbd05/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6772616e74686f6c6c652f6c61726176656c2d6d6f64656c2d66696c746572732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/grantholle/laravel-model-filters/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/24ba3cf3bbed49d358422551185cc4e9d36db12e21fa980683b6ed2086e5d669/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6772616e74686f6c6c652f6c61726176656c2d6d6f64656c2d66696c746572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/grantholle/laravel-model-filters)

A composable way to filter Laravel models. This is not exhaustive, but it can add some basic filtering to your models.

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

[](#installation)

You can install the package via composer:

```
composer require grantholle/laravel-model-filters
```

By default, the package expects that filters are stored in the `f` key of the request. You can change this by adding the environment variable `MODEL_FILTERS_KEY` to your `.env` file.

```
MODEL_FILTERS_KEY=filter

```

Usage
-----

[](#usage)

The first step is registering the filters for the desired model. In the model, add the `HasFilters` trait and define the filters in the `filters` method.

```
use GrantHolle\ModelFilters\Enums\Component;
use GrantHolle\ModelFilters\Filters\MultipleSelectFilter;
use GrantHolle\ModelFilters\Filters\TextFilter;
use GrantHolle\ModelFilters\Traits\HasFilters;

class User extends Authenticatable implements ExistsInSis
{
    use HasFilters;

    public function filters(): array
    {
        return [
            TextFilter::make('search', __('Search'))
                // Exclude from the list of available filters (see below when not present on `availableFiltersToArray()`
                ->hide()
                // By default, the filter will try to construct the query based on the supplied operator and value.
                // If that doesn't meet your needs, you can define the query parameters yourself. It should
                // return an instance of `Illuminate\Database\Eloquent\Builder`.
                ->using(fn (Builder $builder, string $search) => $builder->search($search)),
            // The first argument is the key that will be used to filter the model. The second argument is the label
            TextFilter::make('first_name', __('First name')),
            TextFilter::make('last_name', __('Last name')),
            MultipleSelectFilter::make('user_type', __('Checkbox group'))
                // For filters that can have multiple values/choices, you can
                // define the options. How it's constructed is up to you, since
                // the frontend is implemented independently.
                ->options(UserType::options()),
            MultipleSelectFilter::make('user_type', __('Combobox'))
                ->withComponent(Component::combobox)
                ->options(UserType::options()),
        ];
    }
}
```

Once your filters are defined, you can get the list of the available filters by calling the `availableFiltersToArray` on the model. This allows you to implement the frontend however you want.

```
(new User())->availableFiltersToArray();

// This is the output
$filters = [
    [
        "key" => "first_name",
        "label" => "First name",
        "component" => "text",
        "operators" => [
            "contains" => "Contains",
            "not_contains" => "Doesn't contain",
            "starts" => "Starts with",
            "not_starts" => "Doesn't start with",
            "ends" => "Ends with",
            "not_ends" => "Doesn't end with",
        ],
        "props" => [],
        "defaultValue" => null,
    ],
    [
        "key" => "last_name",
        "label" => "Last name",
        "component" => "text",
        "operators" => [
            "contains" => "Contains",
            "not_contains" => "Doesn't contain",
            "starts" => "Starts with",
            "not_starts" => "Doesn't start with",
            "ends" => "Ends with",
            "not_ends" => "Doesn't end with",
        ],
        "props" => [],
        "defaultValue" => null,
    ],
    [
        "key" => "user_type",
        "label" => "Checkbox group",
        "component" => "checkbox_group",
        "operators" => [
            "in" => "In",
            "not_in" => "Not in",
        ],
        "props" => [
            "options" => [
                "staff" => "Staff",
                "guardian" => "Contact",
                "student" => "Student",
            ],
        ],
        "defaultValue" => [],
    ],
    [
        "key" => "user_type",
        "label" => "Combobox",
        "component" => "combobox",
        "operators" => [
            "in" => "In",
            "not_in" => "Not in",
        ],
        "props" => [
            "options" => [
                "staff" => "Staff",
                "guardian" => "Contact",
                "student" => "Student",
            ],
        ],
        "defaultValue" => [],
    ],
    [
        "key" => "user_type",
        "label" => "Select",
        "component" => "combobox",
        "operators" => [
            "in" => "In",
            "not_in" => "Not in",
        ],
        "props" => [
            "options" => [
                "staff" => "Staff",
                "guardian" => "Contact",
                "student" => "Student",
            ],
        ],
        "defaultValue" => [],
    ],
];
```

The request should use the key defined in the environment (by default `f`) along with the filter details. Take the following query string:

```
?f[0][key]=first_name&f[0][operator]=starts&f[0][value]=gr

```

This will be expanded in the request to the following:

```
[
    [
        "key" => "first_name",
        "operator" => "starts",
        "value" => "gr"
    ]
]
```

In your controller, you can call `currentFilters` on the request to obtain the filters that should be applied to the model.

```
public function index(Request $request)
{
    $filters = $request->currentFilters();
    $users = User::filter($filters)
        ->get();

    // ...
}
```

If you'd like to filter a model manually, the following structure should be used:

```
use GrantHolle\ModelFilters\Enums\Operator;

$filters = [
    [
        "key" => "first_name", // The key should match the key in the filter definition
        "operator" => "contains", // You can also use the Operator::contains enum
        "value" => "an" // This is the value by which to filter
    ],
    [
        "key" => "first_name",
        "operator" => Operator::not_starts_with,
        "value" => "Gr"
    ]
];

User::filter($filters)->pluck("first_name");
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Grant Holle](https://github.com/grantholle)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance63

Regular maintenance activity

Popularity6

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 51.4% 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 ~99 days

Total

5

Last Release

439d ago

PHP version history (2 changes)1.0.0PHP ^8.1

1.2.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/57ed974235b4a23e6aaf9d9039bff2b0d1268edc0e44ebab6e60e4bf1e6eb144?d=identicon)[grantholle](/maintainers/grantholle)

---

Top Contributors

[![grantholle](https://avatars.githubusercontent.com/u/1189456?v=4)](https://github.com/grantholle "grantholle (19 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (10 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (8 commits)")

---

Tags

laravelGrant Hollelaravel-model-filters

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/grantholle-laravel-model-filters/health.svg)

```
[![Health](https://phpackages.com/badges/grantholle-laravel-model-filters/health.svg)](https://phpackages.com/packages/grantholle-laravel-model-filters)
```

###  Alternatives

[spatie/laravel-site-search

A site search engine

300129.1k](/packages/spatie-laravel-site-search)[vormkracht10/laravel-mails

Laravel Mails can collect everything you might want to track about the mails that has been sent by your Laravel app.

24149.7k](/packages/vormkracht10-laravel-mails)[grantholle/laravel-altcha

A Laravel server implementation for Altcha.

3338.7k1](/packages/grantholle-laravel-altcha)[codewithdennis/filament-price-filter

A simple and customizable price filter for FilamentPHP, allowing users to easily refine results based on specified price ranges.

163.2k](/packages/codewithdennis-filament-price-filter)[eightynine/filament-docs

Elegant documentation system for your Filament application with search, navigation, and markdown support

122.5k1](/packages/eightynine-filament-docs)

PHPackages © 2026

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