PHPackages                             touhidurabir/laravel-filterable - 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. [Search &amp; Filtering](/categories/search)
4. /
5. touhidurabir/laravel-filterable

ActiveLibrary[Search &amp; Filtering](/categories/search)

touhidurabir/laravel-filterable
===============================

A package to filter laravel model based on query params or retrieved model collection

1.0.0(4y ago)18281MITPHPPHP &gt;=7.4.0

Since Sep 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/touhidurabir/laravel-filterable)[ Packagist](https://packagist.org/packages/touhidurabir/laravel-filterable)[ RSS](/packages/touhidurabir-laravel-filterable/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (8)Versions (2)Used By (0)

Laravel Filterable
==================

[](#laravel-filterable)

A package to filter laravel model based on query params or retrived model collection.

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

[](#installation)

Require/Install the package using composer:

```
composer require touhidurabir/laravel-filterable
```

To publish the config file:

```
php artisan vendor:publish --provider="Touhidurabir\Filterable\FilterableServiceProvider" --tag=config
```

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

[](#configuration)

The package comes with a configuration file named **filterable** that has to 2 important configs

### Base Filter Class

[](#base-filter-class)

This is a array that contains the base filter class for both the **Query** and **Collection** filter as :

```
'base_class' => [
    'query'         => \Touhidurabir\Filterable\Bases\BaseQueryFilter::class,
    'collection'    => \Touhidurabir\Filterable\Bases\BaseCollectionFilter::class,
],
```

If one need to even extend it to add more functionality or any custom feaure, can do it and set the base class in the config file .

### Filter Class Namespace

[](#filter-class-namespace)

This config define what would be the default namespace(and the store path) of the generated filter classes for the both the **Query** and **Collection** filter classes as :

```
'filterable_namespace' => [
    'query'         => 'App\\QueryFilters',
    'collection'    => 'App\\CollectionFilters',
],
```

If needed to, one can change the default path from there . but it is also possible to pass a different namespace to the filter generate command to provide a different namespace.

Command
-------

[](#command)

This package includes a handly command to generate filter classes as

```
php artisan make:filter User
```

It will generate 2 class **UserQueryFilter** and **UserCollectionFilter** as per defined namespace in the config file .

This command also includes several handful options to make the filer class generation as flexiable as possible such as

### --filters=

[](#--filters)

By passign comma separated filters, it will put those filters as filterable method in both the query and collection filter class as :

```
php artisan make:filter User --filter=name,email
```

For Query FIlter :

```
public function name($value) {

    // return $this->builder->;
}

public function email($value) {

    // return $this->builder->;
}
```

For Collection Filter :

```
public function name($item, $value) {

}

public function email($item, $value) {

}
```

### --query-suffix=QueryFilter

[](#--query-suffixqueryfilter)

Define what would be query filter class file name and class name suffix .

### --collection-suffix=CollectionFilter

[](#--collection-suffixcollectionfilter)

Define what would be collection filter class file name and class name suffix .

### --no-suffix

[](#--no-suffix)

If passed as switch option or flag, no suffix will be added to query or collection filter class names or files name.

### --only-query

[](#--only-query)

If passed as switch option or flag, will only generate the query filters and omit the collection filter class.

### --only-collection

[](#--only-collection)

If passed as switch option or flag, will only generate the collection filters and omit the query filter class.

### --replace

[](#--replace)

If passed as switch option or flag, will replace the existing file. By defalt if a given file already present, it will not replace it .

Usage
-----

[](#usage)

Generate the filters as

```
php artisan make:filter User --filter=name,email
```

it will generate **UserQueryFilter.php** and **UserCollectionFilter.php** class at the given path as :

```
