Creating Filter Class

The make:filterArtisan command allows you to quickly generate new filter classes for your application. These classes define how queries are filtered based on specific conditions, keeping your controller and model logic clean. By default, the generated file will be placed in the directory defined in the path configuration (default: app/Filters).

php artisan make:filter UserStatus 

This will create a UserStatusFilter class, in app/Filters directory.

📋 Rules for Creating Filters

When creating a filter class, follow these rules to ensure your filters work properly:

  • ! Every filter class is related to a model, you should follow certain directory structure to make elefilter works properly. So we have 3 conditions:

    1. Creating filter class inside main filter folder (default: app/Filters):

    in this condition you should have model name when defining class name, for example if you want to define a status filter class for User model, it should be named UserStatus

    php artisan make:filter UserStatus 
    created class: app/Filters/UserStatusFilter.php 

    2. Creating filter class in subfolder (Model name), Filters/User/Class:

    This is best and recommended structure. For every model you can put all filter classes inisde a folder with name of the model, then you don't need to have model name in your filter class name, an example can be like this:

    php artisan make:filter User/Status 
    created class: app/Filters/User/StatusFilter.php 

    3. Creating filter class inside chain folders

    Attention: If you want to have more folders inside Filters directory, the last folder should be named same as Model named. for example

    php artisan make:filter Order/Invoice/Status 
    created class: app/Filters/Order/Invoice/StatusFilter.php 

    pay attention the model in this example is Invoice and you should set $filter_namespace inside model (model should use Filterable trait)

    <?php namespace App\Models\Order; use Illuminate\Database\Eloquent\Model use EleFilter\Traits\Filterable; class Invoice extends Model { use Filterable; protected $filter_namespace = 'Order/Invoice'; # code continue ...

© 2025 elefilter