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

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

macellan/filter
===============

Makes input and output filtering Eloquent models easy

v1.2.0(6y ago)01.2kMITPHPPHP &gt;=5.4.0

Since Aug 23Pushed 6y agoCompare

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

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

Filter
======

[](#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' => 'upper|trim',
        'city' => 'trim'
    ];

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

```

Can also be used standalone:

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

```

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

[](#installation)

Installable via composer:

```
"bcalik/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::registerFilter('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::unregisterFilter('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)
slug        Str::slug($str)
null        empty($str) ? null : $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 Filter\HasFilters;

    public $fillable = ['line1', 'line2', 'line3', 'city', 'postcode'];
    public $input = [
        'line1' => 'trim',
        'line2' => 'trim',
        'line3' => 'trim',
        'city' => 'trim',
        'postcode' => 'upper|trim',
    ];
    public $output = [
        'city' => 'upper', // 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

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~734 days

Total

4

Last Release

2442d ago

Major Versions

v0.1.0 → v1.0.02017-02-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f4b3832b769a6cbd136895170c0befd945d09ea66a182c80313444360221673?d=identicon)[burakcalik](/maintainers/burakcalik)

![](https://avatars.githubusercontent.com/u/2502080?v=4)[M. Mert Yildiran](/maintainers/mertyildiran)[@mertyildiran](https://github.com/mertyildiran)

---

Top Contributors

[![bcalik](https://avatars.githubusercontent.com/u/9222368?v=4)](https://github.com/bcalik "bcalik (7 commits)")[![rmasters](https://avatars.githubusercontent.com/u/34284?v=4)](https://github.com/rmasters "rmasters (4 commits)")[![mertyildiran](https://avatars.githubusercontent.com/u/2502080?v=4)](https://github.com/mertyildiran "mertyildiran (3 commits)")[![spawn-guy](https://avatars.githubusercontent.com/u/1442562?v=4)](https://github.com/spawn-guy "spawn-guy (2 commits)")

---

Tags

laraveleloquentfilter

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/macellan-filter/health.svg)](https://phpackages.com/packages/macellan-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)
