PHPackages                             ahmed-aliraqi/laravel-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. [Search &amp; Filtering](/categories/search)
4. /
5. ahmed-aliraqi/laravel-filterable

ActiveLibrary[Search &amp; Filtering](/categories/search)

ahmed-aliraqi/laravel-filterable
================================

Elegant query filtering for Laravel Eloquent models

v2.1.2(12mo ago)0484MITPHPPHP ^8.0CI passing

Since Jun 24Pushed 12mo agoCompare

[ Source](https://github.com/ahmed-aliraqi/laravel-filterable)[ Packagist](https://packagist.org/packages/ahmed-aliraqi/laravel-filterable)[ RSS](/packages/ahmed-aliraqi-laravel-filterable/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (4)Dependencies (6)Versions (7)Used By (0)

Laravel Filterable
==================

[](#laravel-filterable)

A simple and elegant way to add filtering capabilities to your Laravel Eloquent models.

[![Latest Version on Packagist](https://camo.githubusercontent.com/bf164f235542a4594ff80b15f93d99a587bc344b2e14231f8b094212d7f2c300/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61686d65642d616c69726171692f6c61726176656c2d66696c74657261626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ahmed-aliraqi/laravel-filterable)[![tests](https://github.com/ahmed-aliraqi/laravel-filterable/actions/workflows/tests.yaml/badge.svg?branch=main)](https://github.com/ahmed-aliraqi/laravel-filterable/actions/workflows/tests.yaml)[![Total Downloads](https://camo.githubusercontent.com/1781c3e8b10bb68f67fa84d8a9ae6946acfe26970452e31ab8644c198cdbb22d/68747470733a2f2f706f7365722e707567782e6f72672f61686d65642d616c69726171692f6c61726176656c2d66696c74657261626c652f642f746f74616c2e737667)](https://packagist.org/packages/ahmed-aliraqi/laravel-filterable)

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

[](#installation)

You can install the package via composer:

```
composer require ahmed-aliraqi/laravel-filterable
```

Usage
-----

[](#usage)

### 1. Create a Filter Class

[](#1-create-a-filter-class)

Use the provided artisan command to create a new filter class:

```
php artisan make:filter UserFilter
```

This will create a new filter class in `app/Http/Filters`:

```
namespace App\Http\Filters;

use AhmedAliraqi\LaravelFilterable\BaseFilter;

class UserFilter extends BaseFilter
{
    protected array $filters = [
        'name',
        'email',
        'age'
    ];

    public function name($value)
    {
        $this->builder->where('name', 'like', "$value%");
    }

    public function email($value)
    {
        $this->builder->where('email', $value);
    }

    public function age($value)
    {
        $this->builder->where('age', $value);
    }
}
```

### 2. Apply the Trait to Your Model

[](#2-apply-the-trait-to-your-model)

Add the `Filterable` trait to your model:

```
use AhmedAliraqi\LaravelFilterable\Filterable;

class User extends Model
{
    use Filterable;

    // Optionally specify the filter class
    protected $filter = UserFilter::class;
}
```

### 3. Use the Filter

[](#3-use-the-filter)

You can now filter your model queries:

```
// In your controller
use App\Http\Filters\UserFilter;

public function index(Request $request)
{
    $users = User::filter(new UserFilter($request->query()))->get();

    return response()->json($users);
}
```

Or if you've specified the filter class in your model:

```
$users = User::filter()->get();
```

### Query Parameters

[](#query-parameters)

Your API endpoints will now automatically respond to query parameters that match your defined filters:

```
GET /api/users?name=John&age=25
```

### Including Relationships

[](#including-relationships)

You can include relationships in your filtered results by defining supported includes in your filter class:

```
class UserFilter extends BaseFilter
{
    protected array $filters = ['name', 'email'];

    protected array $supportedInclude = [
        'posts',
        'comments',
        'posts.comments' // Nested relationships
    ];
}
```

Then use the include parameter in your requests:

#### Include single relation

[](#include-single-relation)

```
GET /api/users?include=posts
```

#### Include multiple relations (comma-separated)

[](#include-multiple-relations-comma-separated)

```
GET /api/users?include=posts,comments
```

#### Include nested relations (dot notation)

[](#include-nested-relations-dot-notation)

```
GET /api/users?include=posts.comments
```

You can also load includes on an existing model instance:

```
$user = User::find(1);
$user->loadIncludes(new UserFilter(['include' => 'posts,comments']));
```

Advanced Usage
--------------

[](#advanced-usage)

### Custom Namespaces

[](#custom-namespaces)

You can create filters in custom namespaces:

```
php artisan make:filter Filters/CustomFilter
```

### Complex Filters

[](#complex-filters)

You can define complex filter methods:

```
class UserFilter extends BaseFilter
{
    protected array $filters = [
        'age_range',
        'created_between'
    ];

    public function ageRange($value)
    {
        [$min, $max] = explode(',', $value);

        $this->builder->whereBetween('age', [$min, $max]);
    }

    public function createdBetween($value)
    {
        [$start, $end] = explode(',', $value);

        $this->builder->whereBetween('created_at', [$start, $end]);
    }
}
```

### Filter Arrays

[](#filter-arrays)

You can handle array inputs in your filters:

```
class UserFilter extends BaseFilter
{
    protected array $filters = ['status'];

    public function status(array $value)
    {
        $this->builder->whereIn('status', $value);
    }
}
```

Testing
-------

[](#testing)

```
composer test
```

Authors
-------

[](#authors)

- [Ahmed Fathy](https://github.com/ahmed-aliraqi)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance50

Moderate activity, may be stable

Popularity15

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

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

Every ~1 days

Total

6

Last Release

363d ago

Major Versions

v1.0.0 → v2.0.02025-06-24

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/23261109?v=4)[Ahmed Fathy](/maintainers/ahmed-aliraqi)[@ahmed-aliraqi](https://github.com/ahmed-aliraqi)

---

Top Contributors

[![ahmed-aliraqi](https://avatars.githubusercontent.com/u/23261109?v=4)](https://github.com/ahmed-aliraqi "ahmed-aliraqi (15 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ahmed-aliraqi-laravel-filterable/health.svg)

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

###  Alternatives

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k51.0M7.6k](/packages/larastan-larastan)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

9782.1M162](/packages/laravel-ai)[calebdw/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

15104.9k4](/packages/calebdw-larastan)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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