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

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

balfour/laravel-query-filters
=============================

A library for applying filters &amp; sorts to a query builder

0.0.1-alpha(6y ago)017MITPHP

Since Feb 24Pushed 3y ago5 watchersCompare

[ Source](https://github.com/balfour-group/laravel-query-filters)[ Packagist](https://packagist.org/packages/balfour/laravel-query-filters)[ RSS](/packages/balfour-laravel-query-filters/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (1)Versions (7)Used By (0)

laravel-query-filters
=====================

[](#laravel-query-filters)

A library for applying filters &amp; sorts to a query builder.

*This library is in early release and is pending unit tests.*

Use Case
--------

[](#use-case)

A typical use case is that you have a table with form fields which are used to filter or sort the table data. The filter &amp; sort values are passed as query string parameters, parsed, and are then applied to a database query or eloquent model.

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

[](#installation)

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

Usage
-----

[](#usage)

```
use App\Models\User;
use Balfour\LaravelQueryFilters\FilterSet;
use Balfour\LaravelQueryFilters\Filters\GreaterThanFilter;
use Balfour\LaravelQueryFilters\Filters\MatchesFilter;
use Balfour\LaravelQueryFilters\Filters\RequiresPermission;
use Balfour\LaravelQueryFilters\Sort;

$filters = [
    // the model field defaults to the key if not specified
    new GreaterThanFilter('age'),

    // here, we expect an input parameter 'a' which filters against the 'age' field
    new GreaterThanFilter('a', 'age'),

    new MatchesFilter('fname', 'first_name'),

    // if the year input isn't present, or is empty, a default filter value of 1987 is used
    new MatchesFilter('year', 'year', 1987),

    new MatchesFilter('y', 'year', function () {
        // the default value can also be a closure or callable
        return 1987;
    }),

    // here we use a decorator to restrict this filter to users with the 'filter-by-email' permission
    new RequiresPermission(
        new MatchesFilter('email'),
        'filter-by-email',
    ),

    // we can force a default value if the user doesn't have permission
    // the user must have the 'filter-by-sales-consultant' permission to filter on this
    // value; otherwise, we force it to their user_id
    new RequiresPermission(
        new MatchesFilter('sales_consultant_id'),
        'filter-by-sales-consultant',
        auth()->user()->id,
    ),
];

$sorts = [
    new Sort('fname', 'first_name'),
    new Sort('fname_desc', 'first_name', 'desc'),
    new Sort('last_name'),
    new Sort('name_asc', ['first_name', 'last_name'], 'asc'),
    new Sort('name_desc', ['first_name', 'last_name'], 'desc'),
];

$set = new FilterSet($filters, $sorts, 'fname');

// the default sort can be the key of a valid sort, an instance of a sort or a callable
// which resolves to an instance of a sort
$defaultSort = new Sort('age');

$set = new FilterSet($filters, $sorts, $defaultSort);
// or
$set = new FilterSet($filters, $sorts, function () {
    return new Sort('age');
});

// you can also apply a query callback to the set
// this callback can be used to manipulate the query after all filters and sorts are applied
$set = new FilterSet($filters, $sorts, 'fname', function ($query) {
    $query->where('is_admin', true);
});

// now let's apply a set of filter params
$params = [
    'age' => 13,
    'fname' => 'Matthew',
    'sales_consultant_id' => 123,
    'sort' => 'last_name',
    'sort_dir' => 'desc',
];
$query = User::query();
$set->apply($query, $params);
$results = $query->get();
// or
$results = $query->paginate();

// we can take the query string parameters directly from a request
$query = User::query();
$set->apply($query, request()->all());
$results = $query->all();
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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

2270d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/054093c4138d9bea138f6226b632f8f7ad0adb516562480f7f77868d481e75f0?d=identicon)[balfourgroup](/maintainers/balfourgroup)

---

Top Contributors

[![Gcobani](https://avatars.githubusercontent.com/u/8480669?v=4)](https://github.com/Gcobani "Gcobani (9 commits)")[![matthewgoslett](https://avatars.githubusercontent.com/u/1571743?v=4)](https://github.com/matthewgoslett "matthewgoslett (8 commits)")[![nicja](https://avatars.githubusercontent.com/u/2102143?v=4)](https://github.com/nicja "nicja (2 commits)")

### Embed Badge

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

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

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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