spatie / laravel-query-builder

Easily build Eloquent queries from API requests

Home Page:https://spatie.be/docs/laravel-query-builder

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Zero ignored as a filter value

FZ-Iskandar opened this issue · comments

Hi,

First of all, thank you for this amazing package. It saved me a lot of time and it works really well. It has almost everything covered.

Still, there are some edge cases where it seems to fail.

In my app, I have a datatable of users and some filtering fields. One of them is a selector that lets you filter by "blocked status" to get only active, inactive(blocked) or both type of accounts. The field in the DB is a Mysql Boolean.

The problem here is that when I send the value "0" it gets ignored and so the underlying query is not correctly formed.

I pinpointed where the problem is and I can fix it, but it would imply having to override the package through composer using PSR-4 tricks... an ugly solution.

The problem is in FiltersPartial.php. There you are using array_filter with no parameters and so it assumes al false elements to be eliminated and that means 0 its considered false and eliminated. The solution would be to pass 'strlen' as a callback function as stated here https://stackoverflow.com/a/27501297.

This is not urgent as we can circumvent the problem using a custom filter or a scopeFilter, but I though I could share my experience and see if it can help others

Regards