PHPackages                             mehdirajabi/filterable - 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. mehdirajabi/filterable

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

mehdirajabi/filterable
======================

Filter eloquent model by array query strings

1.0.0(2y ago)6111MITPHP

Since Jul 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/mehdirajabi59/filterable-model)[ Packagist](https://packagist.org/packages/mehdirajabi/filterable)[ RSS](/packages/mehdirajabi-filterable/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Filter Eloquent Models by Array Query Strings
=============================================

[](#filter-eloquent-models-by-array-query-strings)

Introduction
------------

[](#introduction)

The Laravel Filterable Trait is a powerful package providing a reusable trait for easily implementing filter functionality in your Laravel Eloquent models. It enables filtering query results based on multiple parameters, making it simpler to create dynamic and flexible APIs or data filtering mechanisms in your applications.

Features
--------

[](#features)

- Easily implement filtering on Eloquent models.
- Filter data based on various query parameters.
- Support for multiple filter types, such as exact matches, partial matches, ranges, etc.
- Customizable and extensible for adding new filter types.
- Improves code readability and maintainability by separating filtering logic from the main model.

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

[](#installation)

You can install the package via Composer by running the following command:

```
composer require mehdirajabi/filterable
```

Usage
-----

[](#usage)

1. Apply the Filterable Trait to Your Eloquent Model
----------------------------------------------------

[](#1-apply-the-filterable-trait-to-your-eloquent-model)

Open the Eloquent model where you want to apply the filtering functionality and add the Filterable trait:

```
use Mehdirajabi\Filterable\Filterable;

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

In your controller:

```
class UserController extends Controller
{
    public function index()
    {
        return User::filter()->actived()->get();
    }
}
```

2. How Does Filterable Work?
----------------------------

[](#2-how-does-filterable-work)

Two query strings, filterColumns and filterValues, are used, both being arrays. The default operator for all columns is the "like" operator.

For example:

```
http://test.com/api/user/users?filterColumns[0]=name&filterValues[0]=Alex
```

3. Relational Filter
--------------------

[](#3-relational-filter)

To filter on a relation, add the column name as nested. For example, if a user has a relation with a profile and the profile has a 'sex' column, use the following convention:

```
use Mehdirajabi\Filterable\Filterable;

class User extends Model
{
    use Filterable;

    public function profile()
    {
        return $this->hasOne(UserProfile::class);
    }
}
```

```
http://test.com/api/user/users?filterColumns[0]=profile.sex&filterValues[0]=male
```

4. Change Filter Mode Operator
------------------------------

[](#4-change-filter-mode-operator)

To change the operator of the 'status' column, define the filterMode property:

```
use Mehdirajabi\Filterable\Operators\FilterEqual;

class User extends Model
{
    protected $filterMode = [
        'status' => FilterEqual::class
    ];
}
```

5. Change Operator in Controller
--------------------------------

[](#5-change-operator-in-controller)

```
use Mehdirajabi\Filterable\Operators\FilterBetween;

class UserController extends Controller
{
    public function index()
    {
        return User::filter(['id' => FilterBetween::class])->paginate(100);
    }
}
```

Between Operator
----------------

[](#between-operator)

```
use Mehdirajabi\Filterable\Operators\FilterBetween;

class User extends Model
{
    protected $filterMode = [
        'created_at' => FilterBetween::class
    ];
}
```

```
http://test.com/users?filterColumns[0]=created_at&filterValues[0]=2023-06-23 00:00:00, 2023-06-24 23:59:59
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity43

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

1027d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/022f432aae20ab51a122fab7eb4a3cfd01ed62a47b4e04da95e907f4e63a72f3?d=identicon)[mehdirajabi59](/maintainers/mehdirajabi59)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/mehdirajabi-filterable/health.svg)

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

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

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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