PHPackages                             alifcoder/query-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. [Search &amp; Filtering](/categories/search)
4. /
5. alifcoder/query-filter

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

alifcoder/query-filter
======================

A clean query filter builder for Laravel

v1.1.3(1w ago)036MITPHPPHP &gt;=8.2

Since May 20Pushed 1w ago1 watchersCompare

[ Source](https://github.com/alifcoder/query-filter)[ Packagist](https://packagist.org/packages/alifcoder/query-filter)[ RSS](/packages/alifcoder-query-filter/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (4)Versions (11)Used By (0)

🔍 Alif Query Filter
===================

[](#-alif-query-filter)

A lightweight, clean, and reusable query filtering library for Laravel Eloquent models — built to help you keep your controllers clean and your queries dynamic.

---

✨ Features
----------

[](#-features)

- Chainable, dynamic Eloquent filtering based on request input
- Filters as separate classes — fully testable and reusable
- Built-in `eq`/`ne`/`gt`/`gte`/`lt`/`lte` operations, search, sort, joins and soft-delete handling
- Auto-generated validation rules for your filter `FormRequest`s
- No hardcoded column names — every default column, search/sort field, join and validation rule is driven by the publishable config
- Works out-of-the-box with Laravel

---

📦 Requirements
--------------

[](#-requirements)

- PHP &gt;= 8.2
- Laravel ^11.0 || ^12.0 || ^13.0

---

🚀 Installation
--------------

[](#-installation)

```
composer require alifcoder/query-filter
```

Publish the config (optional, but required if you want to override any default):

```
php artisan vendor:publish --tag=query-filter
```

To remove the published config/lang files later:

```
php artisan query-filter:uninstall
```

---

⚙️ Configuration
----------------

[](#️-configuration)

Everything that used to be hardcoded inside the base filter classes now lives in `config/query-filter.php`, so you never need to touch the package internals to adapt it to your schema.

```
return [

    // Validation rules for the built-in query string parameters
    // (sort, limit, search, pagination, soft-delete toggles, ...).
    'default_filters' => [ /* ... */ ],

    // Logical field name => actual database column name, used by
    // BaseEBFilter's prefix(), index(), isActive(), deletedAt(), createdAt(),
    // updatedAt(), createdBy(), updatedBy() and by the default search/sort
    // fields below. Override a value if your table uses a different column
    // name — no need to override the base class.
    'columns' => [
        'prefix'        => 'prefix',
        'index'         => 'index',
        'is_active'     => 'is_active',
        'deleted_at'    => 'deleted_at',
        'created_at'    => 'created_at',
        'updated_at'    => 'updated_at',
        'created_by_id' => 'created_by_id',
        'updated_by_id' => 'updated_by_id',
        // ...
    ],

    // Field keys automatically registered as searchable/sortable for every
    // filter, on top of whatever searchFields()/sortFields() return.
    'default_search_fields' => ['prefix-index', 'prefix', 'index', /* ... */],
    'default_sort_fields'   => ['prefix-index', 'index', 'prefix', /* ... */],

    // Relations resolved by checkJoin() and used to build the
    // "created_by.name" / "updated_by.name" default fields above.
    'default_joins' => [
        'created_by' => [
            'table'        => 'users as created_by',
            'first'        => 'created_by.id',
            'second'       => '{table}.created_by_id',
            'alias'        => 'created_by',
            'name_columns' => ['first_name', 'last_name'],
        ],
        // ...
    ],

    // Extra ValidationRuleDTO definitions merged into every FormRequest that
    // uses FilterPrepareForRequestTrait::getFields().
    'default_validation_fields' => [
        ['field' => 'prefix', 'rules' => ['string'], 'operations' => ['eq', 'ne']],
        ['field' => 'index', 'rules' => ['string'], 'operations' => 'all'],
        // ...
    ],
];
```

Set a `columns` entry, `default_search_fields`/`default_sort_fields` entry, or `default_joins` entry to remove it entirely if a default doesn't apply to your table — the base class only ever registers what's present in config.

---

🧱 Usage
-------

[](#-usage)

### 1. Create a filter

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

```
// app/Filters/PostFilter.php
namespace App\Filters;

use Alif\QueryFilter\Abstracts\BaseEBFilter;
use Alif\QueryFilter\Interfaces\Searchable;
use Illuminate\Database\Eloquent\Builder;

class PostFilter extends BaseEBFilter implements Searchable
{
    protected string $table = 'posts';

    protected function getCallback(): array
    {
        return [
            'is_active'     => [$this, 'isActive'],
            'created_at'    => [$this, 'createdAt'],
            'created_by_id' => [$this, 'createdBy'],
            'search'        => [$this, 'search'],
            'sort'          => [$this, 'sort'],
            'limit'         => [$this, 'limit'],
        ];
    }

    protected function sortFields(): array
    {
        return [
            'title' => $this->table . '.title',
        ];
    }

    protected function joinTables(): array
    {
        return [];
    }

    public function searchFields(string $search): array
    {
        return [
            'title' => $this->table . '.title',
        ];
    }
}
```

### 2. Apply it to a model

[](#2-apply-it-to-a-model)

```
use Alif\QueryFilter\Traits\Filterable;

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

```
use App\Filters\PostFilter;

$posts = Post::filter(new PostFilter($request->validated()))->get();
```

### 3. (Optional) Auto-generate validation rules

[](#3-optional-auto-generate-validation-rules)

```
use Alif\QueryFilter\DTO\ValidationRuleDTO;
use Alif\QueryFilter\Traits\FilterPrepareForRequestTrait;
use Illuminate\Foundation\Http\FormRequest;

class PostIndexRequest extends FormRequest
{
    use FilterPrepareForRequestTrait;

    public function fields(): array
    {
        return [
            new ValidationRuleDTO('title', ['string']),
        ];
    }
}
```

`rules()` will combine your `fields()` with `config('query-filter.default_validation_fields')`and the built-in `default_filters` rules automatically.

---

🌐 Example Query
---------------

[](#-example-query)

```
GET /posts?is_active=1&sort=-created_at&search[title]=hello
```

---

🧩 Folder Structure
------------------

[](#-folder-structure)

```
src/
├── Abstracts/
│   ├── BaseEBFilter.php      # Eloquent Builder filter base class
│   └── BaseQBFilter.php      # Query Builder filter base class
├── Console/
│   └── UninstallQueryFilterCommand.php
├── DTO/
├── Enums/
├── Interfaces/
├── Macros/
├── Traits/
│   ├── Filterable.php
│   └── FilterPrepareForRequestTrait.php
└── QueryFilterServiceProvider.php
config/
└── query-filter.php

```

---

📜 License
---------

[](#-license)

MIT © [Shukhratjon Yuldashev](https://t.me/alif_coder)

###  Health Score

46

—

FairBetter than 92% of packages

Maintenance98

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

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

Recently: every ~1 days

Total

10

Last Release

8d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/94315908?v=4)[Shukhrat Yuldashev](/maintainers/alifcoder)[@alifcoder](https://github.com/alifcoder)

---

Top Contributors

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

### Embed Badge

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

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

PHPackages © 2026

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