PHPackages                             emargareten/eloquent-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. [Database &amp; ORM](/categories/database)
4. /
5. emargareten/eloquent-filters

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

emargareten/eloquent-filters
============================

Easily add filters to your eloquent models using simple arrays

v0.0.13(3mo ago)62.8k[2 PRs](https://github.com/emargareten/eloquent-filters/pulls)MITPHPPHP ^8.1CI failing

Since Apr 27Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/emargareten/eloquent-filters)[ Packagist](https://packagist.org/packages/emargareten/eloquent-filters)[ Docs](https://github.com/emargareten/eloquent-filters)[ RSS](/packages/emargareten-eloquent-filters/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (16)Used By (0)

Eloquent Filters
================

[](#eloquent-filters)

[![Latest Version on Packagist](https://camo.githubusercontent.com/26824148fcecee3305c835cb5b678b8f0074583631a47c00e81751e7bd873ee1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656d61726761726574656e2f656c6f7175656e742d66696c746572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/emargareten/eloquent-filters)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Tests](https://github.com/emargareten/eloquent-filters/actions/workflows/tests.yml/badge.svg)](https://github.com/emargareten/eloquent-filters/actions/workflows/tests.yml)[![Total Downloads](https://camo.githubusercontent.com/8d5d7ff017b29184de9bb7d6dda68358d21d7d53a3e6f8b3a3be0fb52b47cb44/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656d61726761726574656e2f656c6f7175656e742d66696c746572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/emargareten/eloquent-filters)

Eloquent Filterable is a package that helps you filter Laravel Eloquent models using arrays. With this package, you can easily filter Eloquent models based on different criteria and combinations of criteria.

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

[](#requirements)

This package requires PHP 8.1 or later.

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

[](#installation)

You can install the package via composer:

```
composer require emargareten/eloquent-filters
```

Usage
-----

[](#usage)

To get started, you need to add the `Emargareten\EloquentFilterable\Filterable` trait to your model. This trait adds a `filter` method to your model that you can use to filter your model.

Then, add a `filterTypes` property to your model that contains an array of filterable fields. The keys of the array are the names of the fields that can be filtered, and the values are the names of the [filter-type](#filter-types) that should be applied to the field.

```
use Emargareten\EloquentFilterable\Filterable;

class User extends Model
{
    use Filterable;

    public array $filterTypes = [
        'name' => 'string',
        'email' => 'string',
        'age' => 'number',
        'created_at' => 'date',
    ];
}
```

### Filter Types

[](#filter-types)

The following filter types are available by default: `string`, `number`, `boolean`, `date` and `time`.

Each type has a corresponding filter class that is used to filter the field. For example, the `string` type uses the `Emargareten\EloquentFilterable\Filters\StringFilter` class.

To create your own filter types or override/extend existing types you should create a class where each method is a filter operator, the method name should be the operator name (camel case) and the method should accept 3 parameters: `$query`, `$property` and `$value`.

```
namespace App\Filters;

use Emargareten\EloquentFilterable\Filters\StringFilter as BaseStringFilter;

class StringFilter extends BaseStringFilter
{
    public function pattern(Builder $query, string $property, string $value): void
    {
        $query->where($property, 'REGEXP', $value);
    }

    public function notPattern(Builder $query, string $property, string $value): void
    {
        $query->where($property, 'NOT REGEXP', $value);
    }
}
```

Then register your filter class in the `filter_types` config option. (Publish the file with `php artisan vendor:publish --provider="Emargareten\EloquentFilterable\EloquentFilterableServiceProvider"`)

```
return [
    /*
     * The filter classes to be used for each type.
     */
    'filter_types' => [
-        'string' => \Emargareten\EloquentFilters\Filters\StringFilter::class,
+        'string' => \App\Filters\StringFilter::class,
        'number' => \Emargareten\EloquentFilters\Filters\NumberFilter::class,
        'boolean' => \Emargareten\EloquentFilters\Filters\BooleanFilter::class,
        'date' => \Emargareten\EloquentFilters\Filters\DateFilter::class,
        'time' => \Emargareten\EloquentFilters\Filters\TimeFilter::class,
    ],
];
```

### Filtering

[](#filtering)

You can filter your model by calling the `filter` method on your model and passing an array of filters.

Each filter consists of 3 elements `property`, `operator` and `value`.

The `property` is the name of the field that should be filtered, this field has to be defined in the `filterTypes` property of your model (this prevents SQL injection). The `operator` is the operator that should be used to filter the field (it can be in whatever casing you want). The `value` is the value that should be used to filter the field.

```
User::filter([
    [
        'property' => 'name',
        'operator' => 'equal',
        'value' => 'John',
    ],
    [
        'property' => 'age',
        'operator' => 'greater-than',
        'value' => 18,
    ],
])->get();
```

You can override the default operator type by prefixing the operator with the type and a colon (`:`). For example, if you want to use the `string` type for the age field instead of the `number` type, you can use the `string:starts-with` operator.

If you have only one filter, you can pass the filter directly to the `filter` method instead of an array of filters.

```
User::filter([
    'property' => 'name',
    'operator' => 'equal',
    'value' => 'John',
])->get();
```

To group filters using `OR` you can nest the filters in an additional array.

```
User::filter([
    [
        'property' => 'name',
        'operator' => 'equal',
        'value' => 'John',
    ],
    [
        [
            'property' => 'age',
            'operator' => 'less-than',
            'value' => 18,
        ],
        [
            'property' => 'age',
            'operator' => 'greater-than',
            'value' => 60,
        ],
    ],
])->get();
```

The above example will return all users named John that are either younger than 18 or older than 60.

#### Filtering by a relationships

[](#filtering-by-a-relationships)

To filter by a relationship you can use the dot notation to specify the relationship and the field that should be filtered.

```
User::filter([
    [
        'property' => 'posts.title',
        'operator' => 'contains',
        'value' => 'laravel',
    ],
])->get();
```

The above example will return all users that have a post with a title that contains the word laravel.

> **Note**You have to use the `Filterable` trait on the related model in order to filter by relationship.

### Available Operators

[](#available-operators)

#### String Operators

[](#string-operators)

- `exists` - The field value is not null. (you can omit the `value` parameter)
- `not-exists` - The field value is null. (you can omit the `value` parameter)
- `equal` - The field value is equal to the filter value.
- `not-equal` - The field value is not equal to the filter value.
- `starts-with` - The field value starts with the filter value.
- `not-starts-with` - The field value does not start with the filter value.
- `ends-with` - The field value ends with the filter value.
- `not-ends-with` - The field value does not end with the filter value.
- `contains` - The field value contains the filter value.
- `not-contains` - The field value does not contain the filter value.
- `in` - The field value is in the filter value. (`value` should be an array)
- `not-in` - The field value is not in the filter value. (`value` should be an array)

#### Number Operators

[](#number-operators)

- `exists` - The field value is not null. (you can omit the `value` parameter)
- `not-exists` - The field value is null. (you can omit the `value` parameter)
- `equal` - The field value is equal to the filter value.
- `not-equal` - The field value is not equal to the filter value.
- `greater-than` - The field value is greater than the filter value.
- `greater-than-or-equal` - The field value is greater than or equal to the filter value.
- `less-than` - The field value is less than the filter value.
- `less-than-or-equal` - The field value is less than or equal to the filter value.
- `between` - The field value is between the filter value. (`value` should be an array)
- `not-between` - The field value is not between the filter value. (`value` should be an array)
- `in` - The field value is in the filter value. (`value` should be an array)
- `not-in` - The field value is not in the filter value. (`value` should be an array)

#### Boolean Operators

[](#boolean-operators)

- `exists` - The field value is not null. (you can omit the `value` parameter)
- `not-exists` - The field value is null. (you can omit the `value` parameter)
- `equal` - The field value is equal to the filter value.
- `not-equal` - The field value is not equal to the filter value.

#### Date Operators

[](#date-operators)

- `exists` - The field value is not null. (you can omit the `value` parameter)
- `not-exists` - The field value is null. (you can omit the `value` parameter)
- `equal` - The field date is the same date as the filter value.
- `not-equal` - The field date is not the same date as the filter value.
- `greater-than` - The field date is after the filter value.
- `greater-than-or-equal` - The field date is after or equal to the filter value.
- `less-than` - The field date is before the filter value.
- `less-than-or-equal` - The field date is before or equal to the filter value.
- `between` - The field date is between the filter value dates. (`value` should be an array)
- `not-between` - The field date is not between the filter value dates. (`value` should be an array)

#### Time Operators

[](#time-operators)

- `exists` - The field value is not null. (you can omit the `value` parameter)
- `not-exists` - The field value is null. (you can omit the `value` parameter)
- `equal` - The field time is the same time as the filter value.
- `not-equal` - The field time is not the same time as the filter value.
- `greater-than` - The field time is after the filter value.
- `greater-than-or-equal` - The field time is after or equal to the filter value.
- `less-than` - The field time is before the filter value.
- `less-than-or-equal` - The field time is before or equal to the filter value.

### Dynamic Custom Filters

[](#dynamic-custom-filters)

You can define dynamic filters by adding a method to your model prefixed with `filter` (similar to the `scope` prefix for local scopes).

```
use Emargareten\EloquentFilters\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Filterable;

    /**
     * Filter the query by posts that have at least 20 views.
     */
    public function filterHasTwentyViews(Builder $query): void
    {
        $query->where('views', '>=', 20);
    }
}
```

You can also define a dynamic filter that accepts an operator and a value:

```
/**
 * Filter the query by posts that have at least the given amount of views.
 */
public function filterHasViews(Builder $query, string $operator, int $count): void
{
    $query->where('views', $operator, $count);
}
```

You can then use the dynamic filter in your filter array:

```
Post::filter([
    [
        'property' => 'has-twenty-views',
    ],
])->get();

// using a value

Post::filter([
    [
        'property' => 'has-minimum-views',
        'operator' => '>=',
        'value' => 20,
    ],
])->get();
```

Changelog
---------

[](#changelog)

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

Testing
-------

[](#testing)

```
composer test
```

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

[](#contributing)

Contributions are welcome! If you find any bugs or issues or have a feature request, please open a new issue or submit a pull request. Before contributing, please make sure to read the [Contributing Guide](CONTRIBUTING.md).

License
-------

[](#license)

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

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance78

Regular maintenance activity

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 78% 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 ~82 days

Recently: every ~217 days

Total

13

Last Release

119d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d7bb3bbd6b71163ad5f4871aca931336ac3f8c045b9fef5c8b7e0618b62f6efa?d=identicon)[emargareten](/maintainers/emargareten)

---

Top Contributors

[![emargareten](https://avatars.githubusercontent.com/u/46111162?v=4)](https://github.com/emargareten "emargareten (32 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (4 commits)")

---

Tags

laraveleloquentfilters

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/emargareten-eloquent-filters/health.svg)

```
[![Health](https://phpackages.com/badges/emargareten-eloquent-filters/health.svg)](https://phpackages.com/packages/emargareten-eloquent-filters)
```

###  Alternatives

[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[cerbero/query-filters

Filter Laravel Eloquent models based on query parameters.

85282.6k](/packages/cerbero-query-filters)[czim/laravel-filter

Filter for Laravel Eloquent queries, with support for modular filter building

8973.0k3](/packages/czim-laravel-filter)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)

PHPackages © 2026

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