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

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

abdulrehman56/laravel-nested-filter
===================================

A Laravel utility to apply dynamic nested filters and relations from a request.

v1.0.1(7mo ago)1115MITPHPPHP &gt;=7.4

Since Jun 15Pushed 7mo agoCompare

[ Source](https://github.com/AbdulRehman56/Laravel-Nested-Filter)[ Packagist](https://packagist.org/packages/abdulrehman56/laravel-nested-filter)[ RSS](/packages/abdulrehman56-laravel-nested-filter/feed)WikiDiscussions main Synced 1mo ago

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

Advanced Laravel Query Filter Package
=====================================

[](#advanced-laravel-query-filter-package)

A powerful and flexible filtering system for Laravel applications that supports complex database queries with nested relationships, logical operators, and dynamic filtering.

🚀 Features
----------

[](#-features)

- **Simple Filtering**: Basic column-based filtering with operators
- **Nested Relationships**: Filter through related models using dot notation
- **Logical Operators**: Support for AND, OR, and complex nested conditions
- **Relationship Filtering**: Filter records based on relationship existence
- **Dynamic Sorting**: Built-in sorting capabilities
- **Array Support**: Filter using arrays with 'IN' operator
- **Type Safety**: Proper validation and error handling

📦 Installation
--------------

[](#-installation)

```
composer require abdulrehman56/laravel-nested-filter
```

🔧 Basic Usage
-------------

[](#-basic-usage)

### 1. Simple Column Filtering

[](#1-simple-column-filtering)

Filter records based on a single column:

```
// In your controller
$filters = [
    [
        'column_name' => 'name',
        'operator' => '=',
        'value' => 'John Doe'
    ]
];

$request->merge(['filters' => $filters]);
DynamicFilter::applyNestedWhereHas($request, $query);
```

**What this does**: Finds all records where the `name` column equals "John Doe"

### 2. Multiple Conditions with AND

[](#2-multiple-conditions-with-and)

Filter records that meet multiple conditions:

```
$filters = [
    [
        'and' => [
            [
                'column_name' => 'age',
                'operator' => '>=',
                'value' => 18
            ],
            [
                'column_name' => 'status',
                'operator' => '=',
                'value' => 'active'
            ]
        ]
    ]
];
```

**What this does**: Finds records where age is 18 or older AND status is 'active'

### 3. Multiple Conditions with OR

[](#3-multiple-conditions-with-or)

Filter records that meet any of the conditions:

```
$filters = [
    [
        'or' => [
            [
                'column_name' => 'status',
                'operator' => '=',
                'value' => 'active'
            ],
            [
                'column_name' => 'status',
                'operator' => '=',
                'value' => 'pending'
            ]
        ]
    ]
];
```

**What this does**: Finds records where status is either 'active' OR 'pending'

🔗 Relationship Filtering
------------------------

[](#-relationship-filtering)

### 1. Filter Through Related Models

[](#1-filter-through-related-models)

Filter records based on related model data:

```
$filters = [
    [
        'column_name' => 'department.name',
        'operator' => '=',
        'value' => 'Engineering'
    ]
];
```

**What this does**: Finds all records where the related department's name is 'Engineering'

### 2. Deep Nested Relationships

[](#2-deep-nested-relationships)

Filter through multiple levels of relationships:

```
$filters = [
    [
        'column_name' => 'company.department.manager.name',
        'operator' => '=',
        'value' => 'Jane Smith'
    ]
];
```

**What this does**: Finds records where the company's department's manager's name is 'Jane Smith'

### 3. Complex Relationship Conditions

[](#3-complex-relationship-conditions)

Combine multiple conditions within relationships:

```
$filters = [
    [
        'column_name' => 'orders',
        'and' => [
            [
                'column_name' => 'orders.total',
                'operator' => '>=',
                'value' => 100
            ],
            [
                'column_name' => 'orders.status',
                'operator' => '=',
                'value' => 'completed'
            ]
        ]
    ]
];
```

**What this does**: Finds records that have orders with total &gt;= 100 AND status = 'completed'

🔍 Advanced Filtering Options
----------------------------

[](#-advanced-filtering-options)

### 1. Array-Based Filtering (IN Operator)

[](#1-array-based-filtering-in-operator)

Filter records where a column value is in a list:

```
$filters = [
    [
        'column_name' => 'category',
        'operator' => 'in',
        'value' => ['electronics', 'books', 'clothing']
    ]
];
```

**What this does**: Finds records where category is any of: electronics, books, or clothing

### 2. Relationship Existence Filtering

[](#2-relationship-existence-filtering)

Filter records that have or don't have certain relationships:

```
// Records that HAVE a specific relationship
$filters = [
    [
        'have' => 'orders'
    ]
];

// Records that DON'T HAVE a specific relationship
$filters = [
    [
        'does_not_have' => 'orders'
    ]
];
```

**What this does**:

- First example: Finds records that have at least one order
- Second example: Finds records that have no orders

### 3. Complex Nested Logic

[](#3-complex-nested-logic)

Combine multiple logical operations:

```
$filters = [
    [
        'or' => [
            [
                'column_name' => 'status',
                'operator' => '=',
                'value' => 'active'
            ],
            [
                'and' => [
                    [
                        'column_name' => 'age',
                        'operator' => '>=',
                        'value' => 18
                    ],
                    [
                        'column_name' => 'verified',
                        'operator' => '=',
                        'value' => true
                    ]
                ]
            ]
        ]
    ]
];
```

**What this does**: Finds records where status is 'active' OR (age &gt;= 18 AND verified = true)

📊 Sorting
---------

[](#-sorting)

Add sorting to your filtered results:

```
$request->merge([
    'sort_by' => 'created_at',
    'sort_order' => 'desc'
]);

DynamicFilter::applyNestedWhereHas($request, $query);
```

**What this does**: Orders the results by created\_at column in descending order

🛠️ Complete Controller Example
------------------------------

[](#️-complete-controller-example)

Here's how to use the filter system in a Laravel controller:

```
