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

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

baraadark/laravel-filter
========================

LaravelFilter is a package designed to simplify the process of filtering table fields in a Laravel project. It provides a straightforward way to implement custom query filters for your models.

1.0.4(1y ago)124MITPHP

Since Jul 11Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Baraa-Darkazalli/LaravelFilter)[ Packagist](https://packagist.org/packages/baraadark/laravel-filter)[ RSS](/packages/baraadark-laravel-filter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (6)Used By (0)

LaravelFilter
=============

[](#laravelfilter)

LaravelFilter is a package designed to simplify the process of filtering table fields in a Laravel project. It provides a straightforward way to implement custom query filters for your models.

[![Latest Version on Packagist](https://camo.githubusercontent.com/de5233d4e6fff16ef1546c8037a732fd8cfef5f56952aa735612e1a7a55b01cf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62617261614461726b2f6c61726176656c2d66696c7465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/baraadark/laravel-filter)[![Packagist License](https://camo.githubusercontent.com/2a502ae8049ce4d138da6823c3a176ab7e399fca1ce2743193b2cb283b764b77/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f62617261616461726b2f6c61726176656c2d66696c746572)](https://camo.githubusercontent.com/2a502ae8049ce4d138da6823c3a176ab7e399fca1ce2743193b2cb283b764b77/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f62617261616461726b2f6c61726176656c2d66696c746572)[![Total Downloads](https://camo.githubusercontent.com/2884f4480f580ed8eb2e1c8739fb17cecb2b15bb6c09019b828366a0a305b05e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62617261616461726b2f6c61726176656c2d66696c7465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/baraadark/laravel-filter)

---

Getting started
===============

[](#getting-started)

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

[](#installation)

Please check the official laravel installation guide for server requirements before you start. [Official Documentation](https://laravel.com/docs/10.x/installation)

You can install the package via composer:

```
composer require baraadark/laravel-filter
```

Next, publish the configuration file:

```
php artisan vendor:publish --tag=config
```

Usage
-----

[](#usage)

### Applying the Filterable Trait

[](#applying-the-filterable-trait)

Use the Filterable trait in your models to enable filtering.

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use BaraaDark\LaravelFilter\Traits\Filterable;

class YourModel extends Model
{
    use Filterable;
}
```

### Override filterKeys function

[](#override-filterkeys-function)

You should override the filtersKeys method to return an array of filter keys and their corresponding filter classes.

**Example:**

```
use BaraaDark\LaravelFilter\Traits\Filterable;

class YourModel extends Model
{
    use Filterable;

    public function filtersKeys(): array
    {
        return [
            // 'filter-key' => FilterClass::class
        ];
    }
}
```

The filtersKeys method returns an associative array where the keys are the names of the filter keys expected from the request, and the values are the filter classes that contain the query logic.

### Creating a Filter Class

[](#creating-a-filter-class)

To create a Filter class, run the command:

```
php artisan make:filter
```

You will be prompted to enter the class name and the related model name. The generated file will be located at App\\Http\\Filters\\ModelName.

### Filter Class Structure

[](#filter-class-structure)

```
namespace App\Http\Filters\ModelName;

use BaraaDark\LaravelFilter\Filter;

class FilterClass extends Filter
{
    /**
     * Get the validation rules that apply to the filter request.
     *
     * @return array
     */
    public static function rules(): array
    {
        return [];
    }

    /**
     * Apply filter query on the related model.
     *
     * @param \Illuminate\Database\Eloquent\Builder &$query
     */
    public function apply(&$query)
    {
        return $query;
    }
}
```

**Example Filter Class:**

```
use BaraaDark\LaravelFilter\Filter;

class ProductPriceRangeFilter extends Filter
{
    /**
     * Get the validation rules that apply to the filter request.
     *
     * @return array
     */
    public static function rules(): array
    {
        return [
            'min'   => ['required', 'numeric', 'min:0'],
            'max'   => ['required', 'numeric']
        ];
    }

    /**
     * Apply filter query on related model.
     * @param  \Illuminate\Database\Eloquent\Builder &$query
     */
    public function apply(&$query)
    {
        return $query->where('price', '>=', $this->min)
            ->where('price', '
