PHPackages                             jeidison/model-filtrable - 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. jeidison/model-filtrable

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

jeidison/model-filtrable
========================

Filter Eloquent Models and Relationships

0.4(5y ago)030[6 issues](https://github.com/jeidison/model-filtrable/issues)MITPHPPHP ^7.2|^8.0CI failing

Since Mar 9Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jeidison/model-filtrable)[ Packagist](https://packagist.org/packages/jeidison/model-filtrable)[ RSS](/packages/jeidison-model-filtrable/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

Model Filtrable
===============

[](#model-filtrable)

### Installation

[](#installation)

```
$ composer require jeidison/model-filtrable
```

Usage
=====

[](#usage)

#### Add Trait in model

[](#add-trait-in-model)

```
...

use Jeidison\Filtrable\Filtrable;

class Professional extends Model
{
    use Filtrable;

    ...
    protected $fillable = [
        //All fields filtrable;
    ];
    ...

    public function places()
    {
        return $this->belongsToMany(Place::class, 'prof_place', 'id_prof', 'id_place');
    }

    public function specialties()
    {
        return $this->belongsToMany(Specialty::class, 'prof_spec', 'id_prof', 'id_spec');
    }
}
```

#### Service

[](#service)

```
...

use App\Models\Professional;

class ProfessionalService implements IProfessionalService
{
    public function filter()
    {
         return Professional::filter(request()->all())->get();
        // or
         return Professional::filter(['field_one' => 'value1'])->get();
    }
}
```

Querying at the API-endpoint
----------------------------

[](#querying-at-the-api-endpoint)

### Filtering:

[](#filtering)

Where

```
    /api/professionals?id_prof=1
    or
    /api/professionals?id_prof:==1
    or
    /api/professionals?id_prof:=1
    or
    /api/professionals?id_prof:!==1
    or
    /api/professionals?id_prof:>=1
    or
    /api/professionals?id_prof:=100&prof_name:orWhere=Jeidison

```

With:

```
    /api/professionals?with=places,specialties

```

Has:

```
    /api/professionals?has=places

```

Relationship:

```
    /api/places->id_place=1
    /api/places->professionals->id_place=1
    /api/places->professionals->id_place:=1
    /api/places->professionals->id_place:
