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

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

rmasters/filter
===============

Makes input and output filtering Eloquent models easy

v0.1.0(12y ago)211.5k7[1 issues](https://github.com/rmasters/filter/issues)[1 PRs](https://github.com/rmasters/filter/pulls)MITPHPPHP &gt;=5.4.0

Since Aug 23Pushed 5y ago3 watchersCompare

[ Source](https://github.com/rmasters/filter)[ Packagist](https://packagist.org/packages/rmasters/filter)[ Docs](https://github.com/rmasters/filter)[ RSS](/packages/rmasters-filter/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (6)Versions (3)Used By (0)

Filter [![Latest Stable Version](https://camo.githubusercontent.com/b4089ea9b844491694b98b4b0cbd31aeeb071ade0976aabd9a831ad504a51857/68747470733a2f2f706f7365722e707567782e6f72672f726d6173746572732f66696c7465722f762f737461626c652e706e67)](https://packagist.org/packages/rmasters/filter) [![master](https://camo.githubusercontent.com/2d0f06f943e4df319a2b403553e00d64324ef45703c416b35bdd9a15b93a8599/68747470733a2f2f7472617669732d63692e6f72672f726d6173746572732f66696c7465722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/rmasters/filter) [![Coverage Status](https://camo.githubusercontent.com/342c60e3de339dc8c6be4aedea4e96fb809825c8cff6e4dec0463c68290fb205/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f726d6173746572732f66696c7465722f62616467652e706e67)](https://coveralls.io/r/rmasters/filter) [![versioneye dependencies](https://camo.githubusercontent.com/b9b0df2530796176952a7739affd5a2cfba2c8a82ef3fc064892373f6502a3f1/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3532313762616438363332626163373735303033623734392f62616467652e706e67)](https://www.versioneye.com/user/projects/5217bad8632bac775003b749)
===========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#filter----)

Aims to help make filtering input to your Eloquent models easier.

Simplifies code like this:

```
class Address extends Model {
    public function setPostcodeAttribute($value) {
        $this->attributes['postcode'] = strtoupper(trim($value));
    }

    public function setCityAttribute($value) {
        $this->attributes['city'] = trim($value);
    }

    public function getCityAttribute($value) {
        return strtoupper($value);
    }
}

```

Into this:

```
class Address extends Model {
    use Filter\HasFilters

    protected $input = [
        'postcode' => 'uppercase|trim',
        'city' => 'trim'
    ];

    protected $output = [
        'city' => 'uppercase'
    ];
}

```

Can also be used standalone:

```
$clean = Filter::filter(['city' => 'London'], ['city' => 'trim|uppercase']);

```

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

[](#installation)

Installable via composer:

```
"rmasters/filter": "dev-master",

```

### Laravel 4

[](#laravel-4)

To use the model trait and service for Laravel 4, add the following lines to `config/app.php`:

```
'providers' => array(
    // ...
    'Filter\FilterServiceProvider',

'aliases' => array(
    // ...
    'Filter' => 'Filter\Facades\Filter',

```

Usage
-----

[](#usage)

> Examples below use the Facade style (`Filter::filter()`) for brevity - standalone users should expand this to `$filter->filter()`.

The standalone class is similar to Laravel's validator component:

```
$filtered = Filter::filter(['name' => 'Ross'], ['name' => 'trim']);
$value = Filter::filterOne('Ross', 'trim');

```

Rules are also constructed similarly to Validator:

```
Filter::filterOne('test', 'trim|upper');
Filter::filterOne('test...', 'rtrim:.');
Filter::filterOne('test', ['trim', 'upper']);

```

Filters are run sequentially from left to right. Arguments are parsed by [`str_getcsv`](http://php.net/str_getcsv) - e.g. to trim commas use `trim:","`.

### Registering filters

[](#registering-filters)

A filter is a callable that accepts the input string and an array of arguments:

```
Filter::register('slugify', function($str, array $args) {
    return preg_replace('/[^a-z0-9]+/', '-', strtolower($str));
});

```

Other callable values are classes that define an `__invoke` method and function names. For example, Zend Framework's filters all implement `__invoke`, so `'Zend\I18n\Filter\Alnum'` is a valid callable.

Filters can be unregistered using `Filter::unregister('slugify')`.

#### Default filters

[](#default-filters)

By default the following filters are registered:

```
trim        trim($str)
trim:|,/    trim($str, '|/');
ltrim       ltrim($str)
ltrim:|,/   ltrim($str, '|/');
rtrim       rtrim($str)
rtrim:|,/   rtrim($str, '|/');
upper       strtoupper($str)
lower       strtolower($str)
capfirst    ucfirst($str)
lowerfirst  lcfirst($str)

```

### Laravel 4

[](#laravel-4-1)

A trait, `HasFilters` is available that modifies `getAttribute` (accessor) and `setAttribute` (mutator) to apply filters to the input or output value.

These filter rules are specified in properties on the model, `$input` and `$output` for mutators and accessors respectively.

```
class Address extends Model {
    use HasFilters;

    public $fillable = ['line1', 'line2', 'line3', 'city', 'postcode'];
    public $input = [
        'line1' => 'trim',
        'line2' => 'trim',
        'line3' => 'trim',
        'city' => 'trim',
        'postcode' => 'uppercase|trim',
    ];
    public $output = [
        'city' => 'uppercase', // Uppercase only for display
    ];
}

```

The filter instance is available using `App::make('filter')`, or via the facade `Filter` depending on your setup in `config/app.php`.

#### Call chain

[](#call-chain)

You can still write your own accessors or mutators which will be applied as well as any filters that have been set. The following chains happen:

- Mutator: `$model->name = 'Ross'` (filters applied **before** your mutator)
    1. `Filter\HasFilters::setAttribute`
    2. `Eloquent\Model::setAttribute`
    3. `Your\Model::setNameAttribute` (if defined)
- Accessor: `echo $model->name` (filters applied **after** your accessor)
    1. `Eloquent\Model::getAttribute`
    2. `Your\Model::getNameAttribute`
    3. `Filter\HasFilters::getAttribute`

You should not need to modify your mutators (they should still store the value in `$this->attributes[$name]`.

License
-------

[](#license)

Released under the MIT license.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

4647d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/32d44b8b36e16d8e06b489556da46a1e839726b0d6003fcd2642a3c94c8069f7?d=identicon)[rmasters](/maintainers/rmasters)

---

Top Contributors

[![rmasters](https://avatars.githubusercontent.com/u/34284?v=4)](https://github.com/rmasters "rmasters (4 commits)")

---

Tags

laraveleloquentfilter

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[mehdi-fathi/eloquent-filter

Eloquent Filter adds custom filters automatically to your Eloquent Models in Laravel.It's easy to use and fully dynamic, just with sending the Query Strings to it.

450191.6k1](/packages/mehdi-fathi-eloquent-filter)[dyrynda/laravel-model-uuid

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

4802.8M8](/packages/dyrynda-laravel-model-uuid)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)[jedrzej/searchable

Searchable trait for Laravel's Eloquent models - filter your models using request parameters

127259.1k5](/packages/jedrzej-searchable)

PHPackages © 2026

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