mehdi-fathi / eloquent-filter

Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.
https://mehdi-fathi.github.io/eloquent-filter/
MIT License
436 stars 42 forks source link

handle Exception #124

Closed Kamleshpaul closed 3 years ago

Kamleshpaul commented 3 years ago

HI Author,

i m using this, it so far is very good and helpful but now i m getting on issue with it.

when i set in model $whiteListFilter = ['id']

it should mean ?id will handle by this library rest user can handle own but now it is looking for ?anykey it should happen if user $whiteListFilter = ['*'] add like this

blow is the code which you need to look

https://github.com/mehdi-fathi/eloquent-filter/blob/c576a1a44c1b51ca371db177e83e9694a75bb72b/src/QueryFilter/Detection/DetectorConditions.php#L77

Thanks you

mehdi-fathi commented 3 years ago

@Kamleshpaul Hi I can't understand what is the problem. * means all fields can be in the filter. Please explain more

Kamleshpaul commented 3 years ago

the problem is how can we remove You must set $field in whiteListFilter this error ?

in production we can't throw this error

mehdi-fathi commented 3 years ago

@Kamleshpaul If you use eloquent filter in your model you must set whiteListFilter array when calling the filter method.

Kamleshpaul commented 3 years ago

@mehdi-fathi
i added $whiteListFilter = ['id'] this in my model

and when i try in route ?foo=baar it is giving me 500 error as You must set $field in whiteListFilter

here i want only id means ?id should only handel by library not foo

mehdi-fathi commented 3 years ago

@Kamleshpaul you didn’t read readme carefully. Eloquent filter has a method to ignore some param. For example User::ignoreRequest(['perpage'])->filter() ->paginate(request()->get('perpage'), ['*'], 'page')

Kamleshpaul commented 3 years ago

ignoreRequest i m using

but my problem is let's say i handel User::ignoreRequest(['foo'])->filter() in this case foo will be ignored by library but if user my enter any words which we cannot predict , it is throwing 500 error

so my point is here

only search for $whiteListFilter these words and exclude all don't throw 500 error like You must set $field in whiteListFilter

or make any config like env=production will not throw 500 error only env=local will throw 500

mehdi-fathi commented 3 years ago

@Kamleshpaul Ok you are right. But you can handle it in your controller. I try to make a feature for this problem in the later version.

Kamleshpaul commented 3 years ago

yes i can handle it but i found it will be easy to skip in main library itself i guess so i created this issue

thank you 😊

mehdi-fathi commented 3 years ago

@Kamleshpaul Thanks

mehdi-fathi commented 3 years ago

@Kamleshpaul update package to new version


User::AcceptRequest(['username','id'])
            ->filter()
            ->paginate(request()->get('perpage'), ['*'], 'page');

Call AcceptRequest will accept requests that you want to use in conditions eloquent filter. For example username and id param will be in the conditions eloquent filter. Just notice you must set $whiteListFilter in Model. This method is useful for query string manipulation by user.

Kamleshpaul commented 3 years ago

@mehdi-fathi thank you so much for this feature , you save my time 👍