Eloquent Filter comes with amazing features

Mehdi Fathi
3 min readApr 16, 2021

The Eloquent Filter 2.x has been released with amazing features recently. This package is for make report pages or makes an advanced query with many query strings. The eloquent filter is the most complete package available. Easy to use and fully dynamic. If you would like to know fundamental more about this package you can see this link medium or Github page.

New features :
-Custom Detection Condition
-Configuring
-Magic Methods

Custom Detection Condition

Sometimes you want to make your own custom condition to make a new query that eloquent filter doesn’t support by default. Good news you can make custom conditions in the eloquent filter from now on. In fact, you can make conditions for the generate new query after a check by that. For example :

We must make two classes. a class detects conditions and another class to generate the query.

  • Step 1: Create a class to detect the condition
  • Step 2: After that create a class to generate the query. In this example we make WhereRelationLikeConditionQuery class:
  • Step 3: You just make the method EloquentFilterCustomDetection for return array detections of the condition in the model.
  • Every query params are going to detect in WhereRelationLikeCondition for the first time after that check by default detection eloquent filter.
  • Make method EloquentFilterCustomDetection in the above example and return array conditions class.
/users/list?username[value]=mehdi&username[limit]=10&username[email]=mehdifathi&username[like_relation_value]=mehdi&count_posts=10

select * from "users"
where exists (select * from "posts" where
"users"."post_id" = "posts"."id"
and "comment" like ?) and "username" <> ? and "email" like ? and "count_posts" = ? limit 10

You just run code User::filter(); to see the result.

  • Note Also you can set custom detection on the fly by use of a method SetCustomDetection. For example :
$users = User::SetCustomDetection([WhereRelationLikeCondition::class])->filter();
  • Note You can disable EloquentFilterCustomDetection on the fly by this code :
User::SetLoadDefaultDetection(false)->filter();

You can set many detection conditions for example:

  • EloquentFilter::getInjectedDetections() get all your custom injected detection.
  • Note Every custom detection will run before detection by default eloquent filter.

Configuring

You can generate a config file to configure Eloquent Filter. Has amazing features for your project.

Publish Config

php artisan vendor:publish --provider="eloquentFilter\QueryFilter\ServiceProvider"

Config

  • You can disable/enable Eloquent Filter in the config file (eloquentFilter.php).

'enabled' => env('EloquentFilter_ENABLED', true),

Eloquent Filter recognizes every param of the query string to pass it. Maybe you have a query string that you don’t want to recognize by Eloquent Filter. You can use of ignoreRequest for his purpose. But we have a clean solution to this problem. You can set param request_filter_key in the config file. Therefore every query string will recognize by request_filter_key param.

'request_filter_key' => '', // filter

For example, if you set 'request_filter_key' => 'filter', that Eloquent Filter recognizes filter query string.

/users/list?filter[email]=mehdifathi.developer@gmail.com

  • You can disable/enable custom detection of Eloquent Filter in the config file (eloquentFilter.php).
'enabled_custom_detection' => env('EloquentFilter_Custom_Detection_ENABLED', true),

Magic Methods

Magic methods are a collection of methods that you can use of them as wrapper in the Eloquent Filter. For example, serialize data before filter or change data in response and others. Now Eloquent Filter have serializeRequestFilter,ResponseFilter.

Request Filter

Eloquent Filter has a magic method for just change requests injected before handling by the eloquent filter. This method is SerializeRequestFilter. You just implement SerializeRequestFilter method in your Model. For example

As above code, you can modify every query params of the Model in the method serializeRequestFilter before run by Eloquent Filter. This is a good method when you want to set user_id or convert date or remove space and others.

Response Filter

Response Filter is a magic method for just change response after handling by Eloquent Filter. This method is ResponseFilter. You should implement ResponseFilter method in your Model. For example

--

--