PHPackages                             7amoood/eloquent-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. 7amoood/eloquent-filter

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

7amoood/eloquent-filter
=======================

A Laravel package providing a powerful Eloquent trait for filtering, sorting, searching, and range queries via request parameters.

v1.0.0(1mo ago)01MITPHPPHP ^8.2

Since Mar 11Pushed 1mo agoCompare

[ Source](https://github.com/7amoood/eloquent-filter)[ Packagist](https://packagist.org/packages/7amoood/eloquent-filter)[ RSS](/packages/7amoood-eloquent-filter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Eloquent Filter
===============

[](#eloquent-filter)

A Laravel package that provides an Eloquent trait for filtering, sorting, searching, and range queries driven entirely by request parameters.

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11 or 12

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

[](#installation)

```
composer require 7amoood/eloquent-filter
```

The service provider is auto-discovered. To publish the config file:

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

Quick Start
-----------

[](#quick-start)

Add the `HasFilter` trait to your model and define which columns are filterable:

```
use Hamoood\EloquentFilter\HasFilter;

class Post extends Model
{
    use HasFilter;

    protected array $sortCols         = ['created_at', 'title'];
    protected array $filterSearchCols = ['title', 'body'];
    protected array $filterCols       = ['status', 'category_id'];
    protected int   $filterLimit      = 50;
}
```

Then call the `filter()` scope in your controller:

```
$posts = Post::query()->filter()->paginate();
```

All filtering is now controlled via query string parameters.

Filter Types
------------

[](#filter-types)

### Sorting

[](#sorting)

Sort results by an allowed column.

```
GET /posts?filter[sort][key]=created_at&filter[sort][type]=desc

```

- `key` must be listed in the model's `$sortCols` array.
- `type` is `asc` or `desc` (defaults to the value in config).

### Search

[](#search)

Search across multiple columns with a single term:

```
GET /posts?filter[search]=laravel

```

Or search specific fields:

```
GET /posts?filter[search][title]=laravel&filter[search][body]=eloquent

```

Searchable columns are defined in `$filterSearchCols`.

### Column Filters

[](#column-filters)

Filter by exact column values (comma-separated for multiple):

```
GET /posts?filter[column][status]=published
GET /posts?filter[column][category_id]=1,2,3

```

Passing an empty value filters for `NULL`:

```
GET /posts?filter[column][deleted_at]=

```

Allowed columns are defined in `$filterCols`.

#### Nested Relationship Filters

[](#nested-relationship-filters)

Filter through relationships using `$filterColsChilds`:

```
protected array $filterColsChilds = [
    'tags' => ['slug', 'name'],
];
```

```
GET /posts?filter[column][tags][slug]=php,laravel

```

### Fetch

[](#fetch)

Fetch specific records by ID (or another column), bypassing any limit:

```
GET /posts?filter[fetch][id]=1,5,12

```

The `$filterFetch` property controls allowed columns (defaults to `['id']`).

### Column Range

[](#column-range)

Filter by a value range on any column (numeric, string, etc.). Supports providing both `from` and `to`, or just one of them:

```
GET /products?filter[range][column][price][from]=100&filter[range][column][price][to]=500
GET /products?filter[range][column][quantity][from]=10
GET /products?filter[range][column][rating][to]=5

```

Allowed columns are defined in `$filterRangeColumn`.

### Date Range

[](#date-range)

Filter by a date range:

```
GET /posts?filter[range][date][created_at][from]=2025-01-01&filter[range][date][created_at][to]=2025-12-31

```

Allowed columns are defined in `$filterRangeDate`.

### Time Range

[](#time-range)

Filter by a time range (handles overnight spans like `22:00` to `06:00`):

```
GET /posts?filter[range][time][starts_at][from]=09:00&filter[range][time][starts_at][to]=17:00

```

Allowed columns are defined in `$filterRangeTime`.

### Limit

[](#limit)

Override the per-page count (capped by `$filterLimit` or the config default):

```
GET /posts?filter[limit]=25

```

Model Properties Reference
--------------------------

[](#model-properties-reference)

PropertyTypeDescription`$sortCols``array`Columns allowed for sorting`$filterSearchCols``array`Columns included in search queries`$filterCols``array`Columns allowed for exact-match filtering`$filterColsChilds``array`Relationship columns for nested filtering`$filterFetch``array`Columns allowed for fetch (default: `['id']`)`$filterRangeColumn``array`Columns allowed for generic value range filtering`$filterRangeDate``array`Columns allowed for date range filtering`$filterRangeTime``array`Columns allowed for time range filtering`$filterLimit``int`Max per-page limit for this model### Column Aliasing

[](#column-aliasing)

All column arrays support aliasing via key-value pairs. The key is the public name used in the request, and the value is the actual database column:

```
protected array $filterCols = [
    'type' => 'category_id',  // ?filter[column][type]=5  queries category_id
    'status',                  // ?filter[column][status]=active  queries status
];
```

Configuration
-------------

[](#configuration)

After publishing, the config file is at `config/eloquent-filter.php`:

```
return [
    // Request parameter key containing filter data
    'request_key' => 'filter',

    // Max rows when model has no $filterLimit (null to disable)
    'default_limit' => 1000,

    // Default sort direction
    'default_sort_direction' => 'asc',
];
```

Custom Request Key
------------------

[](#custom-request-key)

You can override the request key per-call:

```
Post::query()->filter(key: 'f')->paginate();
// reads from ?f[search]=...
```

Dynamic Sort Columns
--------------------

[](#dynamic-sort-columns)

Add sort columns at query time with the `sortCols` scope:

```
Post::query()->sortCols(['views', 'likes'])->filter()->paginate();
```

License
-------

[](#license)

MIT

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance88

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

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

59d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5ed136e21d6ee0b6d092497fd1558888a0712d9acb137dc027ecf63b0e17735c?d=identicon)[7amoood](/maintainers/7amoood)

---

Tags

searchlaraveleloquentqueryfiltersort

### Embed Badge

![Health badge](/badges/7amoood-eloquent-filter/health.svg)

```
[![Health](https://phpackages.com/badges/7amoood-eloquent-filter/health.svg)](https://phpackages.com/packages/7amoood-eloquent-filter)
```

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[mehdi-fathi/eloquent-filter

Eloquent Filter adds custom filters automatically to your Eloquent Models in Laravel.It's easy to use and fully dynamic, just with sending the Query Strings to it.

450191.6k1](/packages/mehdi-fathi-eloquent-filter)[mohammad-fouladgar/eloquent-builder

527189.5k](/packages/mohammad-fouladgar-eloquent-builder)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

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

Laravel 4/5/6 package that allows to dynamically filter, sort and eager load relations for your models using request parameters

105179.0k1](/packages/jedrzej-pimpable)[aldemeery/sieve

A simple, clean and elegant way to filter Eloquent models.

1396.3k](/packages/aldemeery-sieve)

PHPackages © 2026

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