PHPackages                             tiagomichaelsousa/laravelfilters - 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. tiagomichaelsousa/laravelfilters

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

tiagomichaelsousa/laravelfilters
================================

Laravel Filters is a package that allows to filter eloquent models in a clean way.

v1.7.0(11mo ago)6224.7k8[1 PRs](https://github.com/tiagomichaelsousa/LaravelFilters/pulls)MITPHPCI failing

Since Feb 12Pushed 11mo ago2 watchersCompare

[ Source](https://github.com/tiagomichaelsousa/LaravelFilters)[ Packagist](https://packagist.org/packages/tiagomichaelsousa/laravelfilters)[ Docs](https://github.com/tiagomichaelsousa/laravelfilters)[ RSS](/packages/tiagomichaelsousa-laravelfilters/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (12)Used By (0)

 [![Laravel Filters](./docs/logo.png)](./docs/logo.png)

 [![Packagist Version](https://camo.githubusercontent.com/0386b7f6e76a04fa70eaf9e0db01b9efbfe1a930c98bb1c5e318a5512ce64e88/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746961676f6d69636861656c736f7573612f6c61726176656c66696c746572732e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/0386b7f6e76a04fa70eaf9e0db01b9efbfe1a930c98bb1c5e318a5512ce64e88/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746961676f6d69636861656c736f7573612f6c61726176656c66696c746572732e7376673f7374796c653d666c61742d737175617265) [![Packagist Downloads](https://camo.githubusercontent.com/3a473603d5e3deee277802a90613dd86044be479c562f29dcbb6ccb96f395ab7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746961676f6d69636861656c736f7573612f6c61726176656c66696c746572732e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/3a473603d5e3deee277802a90613dd86044be479c562f29dcbb6ccb96f395ab7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746961676f6d69636861656c736f7573612f6c61726176656c66696c746572732e7376673f7374796c653d666c61742d737175617265) [![Build Status](https://camo.githubusercontent.com/403d016e0405b22ec3c5b718da6e1712ab56bbc950f34fcde0ae2afa45d9a471/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f746961676f6d69636861656c736f7573612f6c61726176656c66696c746572732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/403d016e0405b22ec3c5b718da6e1712ab56bbc950f34fcde0ae2afa45d9a471/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f746961676f6d69636861656c736f7573612f6c61726176656c66696c746572732f6d61737465722e7376673f7374796c653d666c61742d737175617265) [![Style Status](https://camo.githubusercontent.com/d91447d2249300c46a4b5cca048f547d4f4e5dab90b91e8b1b0f87e6697e3226/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3234303133333537392f736869656c64)](https://camo.githubusercontent.com/d91447d2249300c46a4b5cca048f547d4f4e5dab90b91e8b1b0f87e6697e3226/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3234303133333537392f736869656c64) [![Licence](https://camo.githubusercontent.com/18710a25d8a43cb2ff39587fe009f53e01f6aeb277543b7e0be309072209a6bd/68747470733a2f2f706f7365722e707567782e6f72672f746961676f6d69636861656c736f7573612f6c61726176656c66696c746572732f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://camo.githubusercontent.com/18710a25d8a43cb2ff39587fe009f53e01f6aeb277543b7e0be309072209a6bd/68747470733a2f2f706f7365722e707567782e6f72672f746961676f6d69636861656c736f7573612f6c61726176656c66696c746572732f6c6963656e73653f666f726d61743d666c61742d737175617265) [![All Contributors](https://camo.githubusercontent.com/4e96de37ea680371089642c8762ee051f42043eb64b50c4119a4e48970a2d434/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f616c6c5f636f6e7472696275746f72732d312d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/4e96de37ea680371089642c8762ee051f42043eb64b50c4119a4e48970a2d434/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f616c6c5f636f6e7472696275746f72732d312d6f72616e67652e7376673f7374796c653d666c61742d737175617265) [![Code Quality](https://camo.githubusercontent.com/8e84c33d9f7ee504b4cb1d5644057c1aead28d71ecc0c39b1dcd93b0a9409cae/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3136326165393538343834373465653739333063393439366461323164303738)](https://camo.githubusercontent.com/8e84c33d9f7ee504b4cb1d5644057c1aead28d71ecc0c39b1dcd93b0a9409cae/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3136326165393538343834373465653739333063393439366461323164303738) [![Code Coverage](https://camo.githubusercontent.com/1b608d1acb836a7e20c32f3a2d72e3715387fccbd7bf5c58b4ae7e5f3a87489a/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f436f7665726167652f3136326165393538343834373465653739333063393439366461323164303738)](https://camo.githubusercontent.com/1b608d1acb836a7e20c32f3a2d72e3715387fccbd7bf5c58b4ae7e5f3a87489a/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f436f7665726167652f3136326165393538343834373465653739333063393439366461323164303738)

---

Laravel Filters is a package based in a [Laracasts](https://laracasts.com/) video made by [JeffreyWay](https://github.com/JeffreyWay). This package allows to filter eloquent models in a clean and simple way.

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

[](#installation)

1. Install the package via Composer:

    ```
    $ composer require tiagomichaelsousa/laravelfilters
    ```

    The package will automatically register its service provider.
2. Optionally, publish the configuration file if you want to change any defaults:

    ```
    php artisan vendor:publish --provider="tiagomichaelsousa\LaravelFilters\LaravelFiltersServiceProvider" --tag="config"
    ```

Usage
-----

[](#usage)

Create the filter

```
$ php artisan make:filter
```

This command will create a new filter class in the namespace defined on the configuration file.

Updating your Eloquent Models
-----------------------------

[](#updating-your-eloquent-models)

Your models should use the Filterable trait, which has two scopes `filter()` and `resolve()`.

```
use tiagomichaelsousa\LaravelFilters\Traits\Filterable;

class User extends Authenticatable
{
    use Filterable;
}
```

The `filter()` method receives the query builder and the instance of the class responsible for the filter methods.

The `resolve()` method works like an helper. It verifies if the request has the query string `paginate=x` and if its present it return the response with pagination, otherwise it will return the data.

Creating your Filters
---------------------

[](#creating-your-filters)

When you make the request to an endpoint the `QueryFilter` class (that `UserFilters` extends from) verifies if there is any method with the name that you sent in the request query string.

The `php artisan make:filter ` comes with a default search method that you can delete if you want. When you make a request to `/api/users?search=Foobar` the `QueryFilter` class will call the search method because the key `search` is present in both the request and `UserFilters` class.

```
use tiagomichaelsousa\LaravelFilters\QueryFilters;

class UserFilters extends QueryFilters
{
    /**
     * Search all.
     *
     * @param  string  $query
     * @return Builder
     */
    public function search($value = '')
    {
        return $this->builder->where('name', 'like', '%' . $value . '%');
    }
}
```

The search method applies the queries to the builder instance. With that said you can combine multiple clauses. For example, if you want the search method to filter the data from the `name` and `last_name` fields on the db just add the `orWhere` clause.

```
/**
 * Search all.
 *
 * @param  string  $query
 * @return Builder
 */
public function search($value = '')
{
    return $this->builder
                ->where('name', 'like', '%' . $value . '%')
                ->orWhere('last_name', 'like', '%' . $value . '%');
}
```

If you want to add more filters, just add a new method to the class, put the logic for the filter and send it trough the request query string.

You can also filter data through eloquent relationships. For example, filter users from a country\_code `/api/users?country=PT`

```
/**
 * Filter by country.
 *
 * @param  string $country
 * @return Builder
 */
public function country($country)
    {
    return $this->builder->whereHas('address', function ($query) use ($country) {
        $query->where('country_code', $country);
    });
}
```

Updating your Controllers
-------------------------

[](#updating-your-controllers)

```
class UserControllerAPI extends Controller
{
    /**
     * Display a listing of the users.
     *
     * @param  \App\Filters\UserFilters  $filters
     * @return \App\Http\Resources\Collections\UserCollection
     */
    public function index(UserFilters $filters)
    {
        $users = User::filter($filters)->resolve();

        return new UserCollection($users);
    }
```

The `filter()` method can be called in every instance of a model that uses the `Filterable` Trait. So imagine that you have a model Meeting and this Meeting has many users. You can filter the users from the Meeting this way:

```
class MeetingUsersControllerAPI extends Controller
{
    /**
     * Display a listing of the users from a meeting.
     *
     * @param  \App\Filters\UserFilters  $filters
     * @return \App\Http\Resources\Collections\UserCollection
     */
    public function index(Meeting $meeting, UserFilters $filters)
    {
        $users = $meeting->users()->filter($filters)->resolve();

        return new UserCollection($users);
    }
```

A filter could be instantiated also by providing an associative array:

```
$users = User::filter(new UserFilters(['search' => 'john']))->resolve();
```

Change log
----------

[](#change-log)

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

Testing
-------

[](#testing)

```
$ composer test
```

### With test coverage

[](#with-test-coverage)

```
$ composer test-report
```

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

[](#contributing)

Please see [contributing.md](contributing.md) for details and a todolist.

Security
--------

[](#security)

If you discover any security related issues, please email the [author](mailto:tiagomichaelsousa@gmail.com) instead of using the issue tracker.

Credits
-------

[](#credits)

- [@tiagomichaelsousa](https://github.com/tiagomichaelsousa)
- [All Contributors](../../contributors)

License
-------

[](#license)

License MIT. Please see the [license file](license.md) for more information.

Code Of Conduct
---------------

[](#code-of-conduct)

Please see the [code of conduct](code_of_conduct.md) for more information.

Contributors ✨
--------------

[](#contributors-)

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

  [![](https://avatars1.githubusercontent.com/u/28356381?v=4?s=100)
**tiagomichaelsousa**](https://github.com/tiagomichaelsousa)
[💻](https://github.com/tiagomichaelsousa/LaravelFilters/commits?author=tiagomichaelsousa "Code") [📖](https://github.com/tiagomichaelsousa/LaravelFilters/commits?author=tiagomichaelsousa "Documentation") [🖋](#content-tiagomichaelsousa "Content") [👀](https://github.com/tiagomichaelsousa/LaravelFilters/pulls?q=is%3Apr+reviewed-by%3Atiagomichaelsousa "Reviewed Pull Requests") [![](https://avatars1.githubusercontent.com/u/15105462?v=4?s=100)
**Rafael Ferreira**](http://www.xgeeks.io)
[📖](https://github.com/tiagomichaelsousa/LaravelFilters/commits?author=RafaelFerreiraTVD "Documentation") [![](https://avatars0.githubusercontent.com/u/11718923?v=4?s=100)
**Filipe Laíns**](https://twitter.com/MissingClara)
[💻](https://github.com/tiagomichaelsousa/LaravelFilters/commits?author=FFY00 "Code") [📖](https://github.com/tiagomichaelsousa/LaravelFilters/commits?author=FFY00 "Documentation") [🖋](#content-FFY00 "Content") [👀](https://github.com/tiagomichaelsousa/LaravelFilters/pulls?q=is%3Apr+reviewed-by%3AFFY00 "Reviewed Pull Requests") [![](https://avatars0.githubusercontent.com/u/3239389?v=4?s=100)
**Devon Mather**](http://devonmather.xyz)
[💻](https://github.com/tiagomichaelsousa/LaravelFilters/commits?author=devonmather "Code") [🖋](#content-devonmather "Content") [👀](https://github.com/tiagomichaelsousa/LaravelFilters/pulls?q=is%3Apr+reviewed-by%3Adevonmather "Reviewed Pull Requests") [![](https://avatars.githubusercontent.com/u/10560238?v=4?s=100)
**Gianni Ciccarelli**](https://github.com/giannicic)
[💻](https://github.com/tiagomichaelsousa/LaravelFilters/commits?author=giannicic "Code") [🖋](#content-giannicic "Content") This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

###  Health Score

45

—

FairBetter than 92% of packages

Maintenance50

Moderate activity, may be stable

Popularity37

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 51.9% 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 ~275 days

Recently: every ~365 days

Total

8

Last Release

352d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/26550bab7dc2128c616cafe07dfaf9e859efe4bf29364a56ef636bc6574e3317?d=identicon)[tiagomichaelsousa](/maintainers/tiagomichaelsousa)

---

Top Contributors

[![tiagomichaelsousa](https://avatars.githubusercontent.com/u/28356381?v=4)](https://github.com/tiagomichaelsousa "tiagomichaelsousa (28 commits)")[![allcontributors[bot]](https://avatars.githubusercontent.com/in/23186?v=4)](https://github.com/allcontributors[bot] "allcontributors[bot] (10 commits)")[![giannicic](https://avatars.githubusercontent.com/u/10560238?v=4)](https://github.com/giannicic "giannicic (5 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (4 commits)")[![RafaelFerreiraTVD](https://avatars.githubusercontent.com/u/15105462?v=4)](https://github.com/RafaelFerreiraTVD "RafaelFerreiraTVD (3 commits)")[![DinisEsteves](https://avatars.githubusercontent.com/u/35270727?v=4)](https://github.com/DinisEsteves "DinisEsteves (2 commits)")[![FFY00](https://avatars.githubusercontent.com/u/11718923?v=4)](https://github.com/FFY00 "FFY00 (1 commits)")[![devoncmather](https://avatars.githubusercontent.com/u/62438577?v=4)](https://github.com/devoncmather "devoncmather (1 commits)")

---

Tags

laravelpackageslaravel-filtersLaravelFilters

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tiagomichaelsousa-laravelfilters/health.svg)

```
[![Health](https://phpackages.com/badges/tiagomichaelsousa-laravelfilters/health.svg)](https://phpackages.com/packages/tiagomichaelsousa-laravelfilters)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[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)[clickbar/laravel-magellan

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

423715.4k1](/packages/clickbar-laravel-magellan)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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