PHPackages                             pricecurrent/laravel-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. pricecurrent/laravel-eloquent-filters

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

pricecurrent/laravel-eloquent-filters
=====================================

Advanced Laravel models filtering capabilities

0.1.5(1y ago)17758.4k↓70.4%13[1 PRs](https://github.com/pricecurrent/laravel-eloquent-filters/pulls)MITPHPPHP ^7.4|^8.0CI passing

Since Sep 24Pushed 1y ago4 watchersCompare

[ Source](https://github.com/pricecurrent/laravel-eloquent-filters)[ Packagist](https://packagist.org/packages/pricecurrent/laravel-eloquent-filters)[ Docs](https://github.com/pricecurrent/laravel-eloquent-filters)[ GitHub Sponsors](https://github.com/pricecurrent)[ RSS](/packages/pricecurrent-laravel-eloquent-filters/feed)WikiDiscussions master Synced today

READMEChangelog (6)Dependencies (5)Versions (7)Used By (0)

Advanced Laravel models filtering capabilities
==============================================

[](#advanced-laravel-models-filtering-capabilities)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b1027751268ea36b6f0f9e7300f44afad4deab3a308f36b13a08ecb3aa6290c0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f707269636563757272656e742f6c61726176656c2d656c6f7175656e742d66696c746572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pricecurrent/laravel-eloquent-filters)[![run-tests](https://github.com/pricecurrent/laravel-eloquent-filters/actions/workflows/run-tests.yml/badge.svg)](https://github.com/pricecurrent/laravel-eloquent-filters/actions/workflows/run-tests.yml)[![GitHub Code Style Action Status](https://github.com/pricecurrent/laravel-eloquent-filters/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/pricecurrent/laravel-eloquent-filters/actions/workflows/php-cs-fixer.yml)[![Total Downloads](https://camo.githubusercontent.com/6f6d330595a4a0cfdd0a1cdfe40db6d2b97c3b550b6e70ad7c5d5706decd3064/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f707269636563757272656e742f6c61726176656c2d656c6f7175656e742d66696c746572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pricecurrent/laravel-eloquent-filters)

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

[](#installation)

You can install the package via composer:

```
composer require pricecurrent/laravel-eloquent-filters
```

Usage
-----

[](#usage)

This package gives you fine-grained control over how you may go about filtering your Eloquent Models.

This package is particularly good when you need to address complex use-cases, implementing filtering on many parameters, using complex logic.

But let's start with simple example:

Consider you have a Product, and you need to filter products by `name`:

```
use App\Filters\NameFilter;
use Pricecurrent\LaravelEloquentFilters\EloquentFilters;

class ProductsController
{
    public function index(Request $request)
    {
        $filters = EloquentFilters::make([new NameFilter($request->name)]);

        $products = Product::filter($filters)->get();
    }
}
```

Generate eloquent-filter

```
php artisan make:eloquent-filter NameFilter
```

This will put your Filter to the app/Filters directory by default. You may prefix the name with the path, like `Models/Product/NameFilter`.

```
php artisan make:eloquent-filter Models/Product/NameFilter
```

You can use the `--field=name` argument to generate your filter with the field name

```
php artisan make:eloquent-filter Models/Product/NameFilter --field=name
```

Here is what your `NameFilter` might look like:

```
use Pricecurrent\LaravelEloquentFilters\AbstractEloquentFilter;
use Illuminate\Database\Eloquent\Builder;

class NameFilter extends AbstractEloquentFilter
{
    protected $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function apply(Builder $query): Builder
    {
        return $query->where('name', 'like', "{$this->name}%");
    }
}
```

Notice how our Filter has no clue it is tied up with a specific Eloquent Model? That means, we can simply re-use it for any other model, where we need to bring in the same name filtering functionality:

```
use App\Filters\NameFilter;
use App\Models\User;
use Pricecurrent\LaravelEloquentFilters\EloquentFilters;

class UsersController
{
    public function index(Request $request)
    {
        $filters = EloquentFilters::make([new NameFilter($request->user_name)]);

        $products = User::filter($filters)->get();
    }
}
```

You can chain methods from the filter as if it was simply an Eloquent Builder method:

```
use App\Filters\NameFilter;
use App\Models\User;
use Pricecurrent\LaravelEloquentFilters\EloquentFilters;

class UsersController
{
    public function index(Request $request)
    {
        $filters = EloquentFilters::make([new NameFilter($request->user_name)]);

        $products = User::query()
            ->filter($filters)
            ->limit(10)
            ->latest()
            ->get();
    }
}
```

To enable filtering capabilities on an Eloquent Model simply import the trait `Filterable`

```
use Pricecurrent\LaravelEloquentFilters\Filterable;
use Illuminate\Database\Eloquent\Model;

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

### More complex use-case

[](#more-complex-use-case)

This approach scales very well when you are dealing with a real-life larger applications where querying data from the DB goes far beyond simple comparison by a name field.

Consider an app where we have Stores with a Location coordinates and we have products in stock and we need to query all products that are in stock in a store that is in 10 miles radius

We may stuff all the logic in the controller with some pseudo-code:

```
class ProductsController
{
    public function index(Request $request)
    {
        $products Product::query()
            ->when($request->in_stock, function ($query) {
                $query->join('product_stock', fn ($q) => $q->on('product_stock.product_id', '=', 'products.id')->where('product_stock.quantity', '>', 0));
            })
            ->when($request->within_radius, function ($query) {
                $coordinates = auth()->user()->getCoordinates();
                $query->join('stores', 'stores.id', '=', 'product_stock.store_id');
                $query->whereRaw('
                    ST_Distance_Sphere(
                        Point(stores.longitude, stores.latitude),
                        Point(?, ?)
                    ) get();

        return response()->json(['data' => $products]);
    }
}
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

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

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

[](#security-vulnerabilities)

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

Credits
-------

[](#credits)

- [Andrew Malinnikov](https://github.com/pricecurrent)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance42

Moderate activity, may be stable

Popularity46

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 86% 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 ~256 days

Recently: every ~295 days

Total

6

Last Release

465d ago

### Community

Maintainers

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

---

Top Contributors

[![pricecurrent](https://avatars.githubusercontent.com/u/4919983?v=4)](https://github.com/pricecurrent "pricecurrent (37 commits)")[![medeiroz](https://avatars.githubusercontent.com/u/11270546?v=4)](https://github.com/medeiroz "medeiroz (2 commits)")[![rkrater](https://avatars.githubusercontent.com/u/1753094?v=4)](https://github.com/rkrater "rkrater (2 commits)")[![SupianIDz](https://avatars.githubusercontent.com/u/37969970?v=4)](https://github.com/SupianIDz "SupianIDz (1 commits)")[![tom-knight](https://avatars.githubusercontent.com/u/1437113?v=4)](https://github.com/tom-knight "tom-knight (1 commits)")

---

Tags

eloquentfilterlaravellaraveleloquent-filterspricecurrentlaravel-eloquent-filtersmodel-filters

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k43.2M632](/packages/spatie-laravel-medialibrary)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[spatie/laravel-health

Monitor the health of a Laravel application

87512.0M167](/packages/spatie-laravel-health)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[clickbar/laravel-magellan

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

440902.9k1](/packages/clickbar-laravel-magellan)

PHPackages © 2026

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