PHPackages                             marksihor/laravel-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. [Database &amp; ORM](/categories/database)
4. /
5. marksihor/laravel-query-filter

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

marksihor/laravel-query-filter
==============================

Laravel Query Filter

1.29(1y ago)14.6k↓100%MITPHPPHP ^8.0.0

Since Feb 22Pushed 1y ago1 watchersCompare

[ Source](https://github.com/marksihor/laravel-query-filter)[ Packagist](https://packagist.org/packages/marksihor/laravel-query-filter)[ RSS](/packages/marksihor-laravel-query-filter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (31)Used By (0)

Laravel Query Filter
--------------------

[](#laravel-query-filter)

Installation#### Install the package via composer:

[](#install-the-package-via-composer)

```
composer require marksihor/laravel-query-filter
```

#### Publish the config files (needed if You're willing to change configs):

[](#publish-the-config-files-needed-if-youre-willing-to-change-configs)

```
php artisan vendor:publish --provider="LaravelQueryFilter\\LaravelQueryFilterServiceProvider" --tag=config
```

Usage#### 1. Add "FiltersQueries" trait to Your Controller.php:

[](#1-add-filtersqueries-trait-to-your-controllerphp)

```
namespace App\Http\Controllers;

...
use LaravelQueryFilter\FiltersQueries;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests, FiltersQueries;
}
```

#### 2. Use "$this-&gt;filter()" method in Your controllers like in example below:

[](#2-use-this-filter-method-in-your-controllers-like-in-example-below)

```
namespace App\Http\Controllers;

...

class PostController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $collection = $this->filter(Post::query())->paginate(20);

        return response()->json([
            'data' => $collection
        ]);
    }
}
```

#### \#### 3. Add public array $filterableColumns = \[\]; // (it can improve querying speed by not using: Schema::hasColumn($table, $column) for checking column existence)

[](#-3-add-public-array-filterablecolumns-----it-can-improve-querying-speed-by-not-using--schemahascolumntable-column-for-checking-column-existence)

```
namespace App\Models;

...

class Post extends Model
{
    public array $filterableColumns = ['id', 'name', 'created_at', 'etc...'];
}
```

ConfigurationsFilter Settings (configs/laravel\_query\_filter.php):
-----------------------------------------------------

[](#filter-settings-configslaravel_query_filterphp)

#### Model Configuration:

[](#model-configuration)

There are two ways to configure Models:

- pass an array of parameters (in this case they will be processed every request)
- pass an anonymous function (in this case extra logic can be provided)

Model settings options (if not provided - the check will not be performed):

- columns - the columns that will be displayed when retrieving records
- relations - the relations that well be allowed to retrieve and filter (empty array - forbids all relations)

```
[
    ...
    'model_settings' => [
        \App\Models\Post::class => function () {
        if (!auth()->check() || !auth()->user()->isAdmin()) {
            return [
                'columns' => ['id', 'title', 'text', 'user_id'],
                'relations' => ['comments', 'user']
            ];
        }
           return [];
        },
        \App\Models\User::class => [
            'columns' => ['id', 'name', 'email'],
            'relations' => []
        ],
        // class based setting, mast implement FilterSettingsInterface
        \App\Models\Customer::class => \App\Http\Filters\CustomerFilterSettings::class
    ]
]
```

#### Filters Configuration:

[](#filters-configuration)

The filters of application are listed in the filters array. To disable specific filter, simply delete the corespondent class from the list. It is easy to add Your own filter:

- Create new filter class in Your application;
- Implement the \\LaravelQueryFilter\\Filters\\FilerInterface interface, and write the logic for the filter;
- Add the created filter to filters list;

#### Publish the config files (needed if You're willing to change configs):

[](#publish-the-config-files-needed-if-youre-willing-to-change-configs-1)

```
php artisan vendor:publish --provider="LaravelQueryFilter\\LaravelQueryFilterServiceProvider" --tag=config
```

Query ExamplesFilter by column (\\LaravelQueryFilter\\Filters\\ColumnValuesFilter::class)
---------------------------------------------------------------------------

[](#filter-by-column-laravelqueryfilterfilterscolumnvaluesfilterclass)

Exact match:

> example.com/api/posts?name=Post1

String that contains the substring (surround the serchable string with % character):

> example.com/api/posts?text=%hello%

Starts with the substring (put % character to the end of the serchable string):

> example.com/api/posts?text=Error%

Ends with the substring (put % character to the start of the serchable string):

> example.com/api/posts?text=%provident.

Json column filter (same syntax to find contains, starts with, ends with):

> example.com/api/posts?data-&gt;name=John
> example.com/api/posts?data\_\_name=John

Filter by reserved words (\\LaravelQueryFilter\\Filters\\ColumnValuesFilter::class)
-----------------------------------------------------------------------------------

[](#filter-by-reserved-words-laravelqueryfilterfilterscolumnvaluesfilterclass)

Records where value is null:

> example.com/api/posts?status=null

Records where value is not null:

> example.com/api/posts?status=notNull

Records where date is today:

> example.com/api/posts?created\_at=today

Records where date is tomorrow:

> example.com/api/posts?created\_at=tomorrow

Records where date is yesterday:

> example.com/api/posts?created\_at=yesterday

Records where date is day beforeyesterday:

> example.com/api/posts?created\_at=day\_before\_yesterday

Records where date is more than or equal current:

> example.com/api/posts?created\_at=future

Records where date is less than or equal current:

> example.com/api/posts?created\_at=past

Records where value is more than or equal to:

> example.com/api/posts?likes\[from\]=100

Records where value is less than or equal to:

> example.com/api/posts?likes\[to\]=200

Records where value is between range:

> example.com/api/posts?likes\[between\]=100,200

Records where value is in the list:

> example.com/api/posts?status\[in\]=active,disabled

Records where value is not in the list:

> example.com/api/posts?status\[not\_in\]=active,disabled

Ordering (\\LaravelQueryFilter\\Filters\\OrderFilter::class)
------------------------------------------------------------

[](#ordering-laravelqueryfilterfiltersorderfilterclass)

Order by asc:

> example.com/api/posts?orderBy=title&amp;order=asc

Order by desc:

> example.com/api/posts?orderBy=title&amp;order=desc

Order by json column:

> example.com/api/posts?orderBy=data\_\_key&amp;order=desc
> example.com/api/posts?orderBy=data-&gt;key&amp;order=asc

Order asc/desc (old way):

> example.com/api/posts?id\[orderBy\]=asc
> example.com/api/posts?id\[orderBy\]=desc

Selecting columns (\\LaravelQueryFilter\\Filters\\SelectColumnsFilter::class)
-----------------------------------------------------------------------------

[](#selecting-columns-laravelqueryfilterfiltersselectcolumnsfilterclass)

Select columns by provided comma separated values:

> example.com/api/posts?select=id,title

Retrieving related records (\\LaravelQueryFilter\\Filters\\WithCountRelationsFilter::class)
-------------------------------------------------------------------------------------------

[](#retrieving-related-records-laravelqueryfilterfilterswithcountrelationsfilterclass)

### Basic

[](#basic)

Direct relations by providing comma separated relation names:

> example.com/api/posts?with=comments,user

Nested relations by providing dot separated relationships structure:

> example.com/api/posts?with=comments.user

### Advanced

[](#advanced)

Direct relations with extra filters (select, order, filter by column):

> example.com/api/posts?with\[comments\]\[select\]=id,text,post\_id&amp;with\[comments\]\[orderBy\]=id&amp;with\[comments\]\[order\] =desc&amp;with\[comments\]\[text\]=%non%

Nested relations with extra filters (select, with):

> example.com/api/posts?with\[user\]\[with\]=comments&amp;with\[user\]\[select\]=id&amp;with\[user\]\[with\]\[comments\]\[select\] =id,post\_id,user\_id&amp;select=id,user\_id

With count relationships (\\LaravelQueryFilter\\Filters\\WithCountRelationsFilter::class)
-----------------------------------------------------------------------------------------

[](#with-count-relationships-laravelqueryfilterfilterswithcountrelationsfilterclass)

### Basic

[](#basic-1)

Count direct relations by providing comma separated relation names:

> example.com/api/posts?withCount=comments,user

### Advanced

[](#advanced-1)

Count direct relations by providing relation and additional filters:

> example.com/api/posts?withCount\[comments\]\[user\_id\]=8

> ## With sum relationships (\\LaravelQueryFilter\\Filters\\WithSumRelationsFilter::class)
>
> [](#with-sum-relationships-laravelqueryfilterfilterswithsumrelationsfilterclass)

### Basic

[](#basic-2)

Sum direct relations by providing comma separated relation names and columns:

> example.com/api/customers?withSum=payments.total,payments\_paid.total

Retrieving records that has relations (\\LaravelQueryFilter\\Filters\\HasRelationsFilter::class)
------------------------------------------------------------------------------------------------

[](#retrieving-records-that-has-relations-laravelqueryfilterfiltershasrelationsfilterclass)

### Basic

[](#basic-3)

By providing comma separated relation names:

> example.com/api/posts?has=comments example.com/api/posts?has=comments.user

### Advanced

[](#advanced-2)

By providing relation names with additional filters:

> example.com/api/posts?has\[comments\]\[id\]=20

Retrieving records that does not have relations (\\LaravelQueryFilter\\Filters\\HasNotRelationsFilter::class)
-------------------------------------------------------------------------------------------------------------

[](#retrieving-records-that-does-not-have-relations-laravelqueryfilterfiltershasnotrelationsfilterclass)

### Basic

[](#basic-4)

By providing comma separated relation names:

> example.com/api/posts?hasNot=comments

### Advanced

[](#advanced-3)

By providing relation names with additional filters:

> example.com/api/posts?hasNot\[comments\]\[id\]=13

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity65

Established project with proven stability

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

Recently: every ~66 days

Total

30

Last Release

716d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/502c6ff875a2fb864db565af2932a45065d754883b6bc3e6819adcc7a326cb22?d=identicon)[marksihor](/maintainers/marksihor)

---

Top Contributors

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

---

Tags

phplaraveleloquentfiltering

### Embed Badge

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

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

###  Alternatives

[sebastiaanluca/laravel-boolean-dates

Automatically convert Eloquent model boolean attributes to dates (and back).

40111.7k1](/packages/sebastiaanluca-laravel-boolean-dates)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3059.0k](/packages/matchory-elasticsearch)[rayvenues/eloquent-model-generator

Eloquent Model Generator

1325.2k1](/packages/rayvenues-eloquent-model-generator)[wayofdev/laravel-cycle-orm-adapter

🔥 A Laravel adapter for CycleORM, providing seamless integration of the Cycle DataMapper ORM for advanced database handling and object mapping in PHP applications.

3516.7k3](/packages/wayofdev-laravel-cycle-orm-adapter)[salehhashemi/laravel-repository

Implementing the repository pattern for Laravel projects.

2010.5k](/packages/salehhashemi-laravel-repository)

PHPackages © 2026

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