Filter Class

In this section, we want to see how should we implement the methods inside filter classes to apply the desired filter. When you extends the modelFilter, You have access to builder object via builder property, Also some predefind query methods, but first, let's check how to define 2 types of methods in filter class.

1. methods without arguments:

These methods don't need any value to use in query conditions. A good example is Status filter for user with 2 conditions of active and deactive. Let's consider any user, that has verified email, is active user, and users with null email_verified_at columns, are deactive users. For making query conditions, we don't need any value from outside. So the filter class can be like this

<?php namespace App\Filters\User; use EleFilter\Database\ModelFilter class StatusFilter extends ModelFilter { public function active() { $this->builder->whereNotNull('email_verified_at'); } public function deactive() { $this->builder->whereNull('email_verified_at'); } }

2. methods with arguments:

These methods need value from outside, a good example is searching. The search process needs to get a value, and make the query based on it. All of class filters like this, has a default method called apply. (you can change this method name in config). You should set a parameter for this method and while callig the filter method on your model (User::filter), instead of passing the method name as array value,should pass the parameter. The sample filter class should be like this

<?php namespace App\Filters\User; use EleFilter\Database\ModelFilter class SearchFilter extends ModelFilter { public function apply($search) { $this->builder->where('email' , 'like' , "%$search%"); } }

What are ModelFilter benefits :

ModelFilter has a property $column. Let's rewrite StatusFilter :

<?php namespace App\Filters\User; use EleFilter\Database\ModelFilter class StatusFilter extends ModelFilter { protected $column = 'email_verified_at'; public function active() { $this->builder->whereNotNull($this->column); } public function deactive() { $this->builder->whereNull($this->column); } }

Below are the full list of ModelFilter methods available to write queries more faster:

Here is an example of implementing a filter method using ModelFilter's built in methods

<?php namespace App\Filters\User; use EleFilter\Database\ModelFilter class StatusFilter extends ModelFilter { protected $column = 'email_verified_at'; public function active() { $this->notNull(); # the value of column is $column but you can modify it by passing an argument } public function deactive() { $this->null(); } }

© 2025 elefilter