PHPackages                             coderaworks/laravel-query-filters - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. coderaworks/laravel-query-filters

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

coderaworks/laravel-query-filters
=================================

Query filters package for Laravel

00PHP

Since Jun 21Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Jacobs63/laravel-query-filters)[ Packagist](https://packagist.org/packages/coderaworks/laravel-query-filters)[ RSS](/packages/coderaworks-laravel-query-filters/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Query Filters
=====================

[](#laravel-query-filters)

This package offers convenient class-defined filters for your eloquent queries.
Each filter can be individually validated, allowing for complete control of your code base and avoiding exposing your database structure to the outside world.
The query filtering supports both the DB facade &amp; the Eloquent query builder.

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

[](#installation)

Install the package via composer, using the terminal:

```
composer require coderaworks/laravel-query-filters
```

Additionally, you may publish the provided translations:

```
php artisan vendor:publish --tag=laravel-query-filters
```

Usage
-----

[](#usage)

### Creating a filter

[](#creating-a-filter)

To create a filter, you simply create a class that implements the `\CoderaWorks\LaravelQueryFilters\Interfaces\QueryFilterInterface` interface:

```
use \CoderaWorks\LaravelQueryFilters\Contract\QueryFilterInterface;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

class ExampleFilter implements QueryFilterInterface
{
    public function pattern(): string
    {
        return 'example-filter';
    }

    public function valid(string $tag, mixed $value, mixed $data): bool
    {
        return true;
    }

    public function apply(Builder|EloquentBuilder $query, string $tag, mixed $value, mixed $data): void
    {
        $query->where('example-field', $value);
    }
}
```

The `pattern` returns a regex pattern that will be used to match the requested filters.
The `valid` method returns whether the provided tag, value &amp; data combination are correct for this filter.
The `apply` method applies the filter to the query in any way you'd like.

### Registering a filter

[](#registering-a-filter)

To register a filter, you simply register it the `QueryFiltersProcessor` in your ServiceProvider, e.g.:

```
use CoderaWorks\LaravelQueryFilters\QueryFiltersProcessor;
use Illuminate\Support\ServiceProvider;

class ExampleServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->afterResolving(
            QueryFiltersProcessor::class,
            function (QueryFiltersProcessor $queryFiltersProcessor) {
                $queryFiltersProcessor->registerFilters(
                    'listing-tag',
                    ExampleFilter::class,
                );
            }
        );
    }
}
```

Using the `afterResolving` with a callback also ensures the listing processor is instantiated lazily, which improves the application's performance whenever you're not actually using the listing processor.

### Applying filters

[](#applying-filters)

To apply filters, you should first create a validated form request class, which implements the `\CoderaWorks\LaravelQueryFilters\Http\Requests\Trait\HasQueryFiltersInRequestTrait` trait, e.g.:

```
use CoderaWorks\LaravelQueryFilters\Http\Requests\Trait\HasQueryFiltersInRequestTrait;
use Illuminate\Foundation\Http\FormRequest;

class ExampleQueryFiltersRequest extends FormRequest
{
    use HasQueryFiltersInRequestTrait;

    public function rules(): array
    {
        return [
            // your custom additional validation rules go here
        ] + $this->getQueryFiltersRules();
    }

    public function queryFiltersList(): string
    {
        return 'example-list';
    }
}
```

The form request must also either implement the `queryFiltersList` method or the `$queryFiltersList` property, which returns the tag of the listing we're about to process.
You should also make sure you return the listing rules provided from the trait using the method `getQueryFiltersRules`.

Then you could use the created &amp; now validated request class in a controller:

```
use CoderaWorks\LaravelQueryFilters\QueryFiltersProcessor;
use CoderaWorks\LaravelQueryFilters\Tests\TestClasses\Http\Requests\QueryFiltersTestRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class ExampleQueryFiltersController
{
    public function __invoke(
        QueryFiltersExampleRequest $request,
    )
    {
        // Get a query instance which we want to filter, e.g. via `DB::table()`:
        $query = DB::table('your-table');

        $query->filter(
            $request->getQueryFiltersList(),
            $request->getPossibleQueryFiltersBag(),
        );

        // Get the query result, e.g. via `get()`:
        $result = $query->get();
    }
}
```

Payload
-------

[](#payload)

You'll likely be pairing a Single Page Application such as Vue when using this package, so you'll want to implement a route for the list. The payload should contain the query filters in the following shape:

```
type Payload = {
    filters: [
        {
            tag: string
            value?: any
            data?: any
        }

    ]
}
```

Json payload example:

```
{
  "filters": [
    {
      "tag": "example-filter",
      "value": "example-value",
      "data": {
        "key": "value"
      }
    },
    {
      "tag": "example-filter-2",
      "value": "example-value-2"
    },
    {
      "tag": "example-filter-3"
    }
  ]
}
```

Examples
--------

[](#examples)

See the [examples](examples) folder for some additional code examples on how to use this package.

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity22

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/49c09daa10debf1ce0d96dc42ac2a97356f17e264af6dfdbaa99e194f4a38b5b?d=identicon)[jacobs63](/maintainers/jacobs63)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/coderaworks-laravel-query-filters/health.svg)

```
[![Health](https://phpackages.com/badges/coderaworks-laravel-query-filters/health.svg)](https://phpackages.com/packages/coderaworks-laravel-query-filters)
```

###  Alternatives

[fungio/google-calendar-bundle

Provides a google calendar integration for your Symfony 3 Project.

1042.2k](/packages/fungio-google-calendar-bundle)[ttree/contentrepositoryimporter

Helper package to import data in the Neos content repository

1510.2k](/packages/ttree-contentrepositoryimporter)

PHPackages © 2026

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