DEV Community

Cover image for Making the advanced query filter with Eloquent Filter in Laravel

Making the advanced query filter with Eloquent Filter in Laravel

Mehdi Fathi on December 22, 2019

Introduction Have you ever experienced make an advanced search page in programming? if your answer is yes maybe you know this feature ha...
Collapse
 
goppi0 profile image
goppi0

Thanks for your efforts. I'm new to laravel and didn't quite get it working. In particular the Request causes me issues. My controller is extended from Controller and has as a parameter Request. If I make all the changes as you outlined I got first of all a complain that Requests is unknown (I added the use statement for Requests back in) and thereafter the complain "Non-static method Illuminate\Http\Request::input() should not be called statically". What am I missing and what;s the solution?

Worth to mention - I'm using the latest Laravel and I'm wondering if that has already some functionality that supersedes your package?

Thanks,
Goppi

Collapse
 
mehdifathi profile image
Mehdi Fathi

Your problem is related to laravel.please read request laravel doc.

Collapse
 
aurelianspodarec profile image
Aurelian Spodarec

Do you think he didn't read before askign? This is quite toxic. I'm learning Laravel myself and I had that error before as well. I read the docs, and I didn't quite understand it, or I missed the docs or etiher didn't understand the docs.

A link pointing to that, and maybe saying what a "static" method is would be more helpful.

Assume that people read the docs when they post such comments...

Collapse
 
goppi0 profile image
goppi0

I have got a parameter in my API call that is not related to a table field and as such should not be used by the filter. I didn't add it to the whitelist (similar like your page parameter) but I'm getting the error "You must set flexibility in whiteListFilter in SpecialOffer\n or create a override method."

I guess I need to create an override method - how do I do that?

thanks
goppi

Collapse
 
mehdifathi profile image
Mehdi Fathi

You must set white list array in your model.you can read this article again.

Collapse
 
jorenthijs profile image
Joren Thijs

I am new to Laravel and am using your package in a school project and it works like a charm👍

I do however wanna give you a heads up about one small bug i have seemed to run into.
When combining your filter with a paginator it correctly ignores the page query parameter:
'my-url.com/api/resource?page=5'

But it doesn't seem to ignore the perPage query parameter:
'my-url.com/api/resource?page=5&perPage=100'
This causes a code 500 response.
With an error asking met to add it to the whitelist. (Laravel V7.0)

Collapse
 
mehdifathi profile image
Mehdi Fathi • Edited

Thanks, you can use this code

$perpage = Request::input('perpage');
Request::offsetUnset('perpage');
$users = User::filter($modelFilters)->with('posts')->orderByDesc('id')->paginate($perpage,['*'],'page');

You can make every param for your self but you can unset before send to filter method model

Collapse
 
alexanderop profile image
Alexander Opalic

Really nice filter we are using at my work too.

Collapse
 
mehdifathi profile image
Mehdi Fathi

Thanks

Collapse
 
vitorhugosg profile image
Vitor Hugo Soares Gonçalves • Edited

very nice!

I tried to use it with paginate, but returned an error saying that the colums page is not on the whitelist, you know how to fix it?

Collapse
 
mehdifathi profile image
Mehdi Fathi

Yes, you are right. I fixed it. You just run the command composer update mehdi-fathi/eloquent-filter to update that. If you have any problem you should read the readme in Github or comment that under this post. Thanks for your Response.

Collapse
 
robsoncasousa profile image
Robson Cavalcante

Illuminate \ Contracts \ Container \ BindingResolutionException
Target class [eloquentFilter] does not exist.

Do you how to fix this?
I followed your github respository install instructions

Collapse
 
robsoncasousa profile image
Robson Cavalcante

never mind.. just cache: php artisan config:cache

Collapse
 
saber13812002 profile image
Saber tabatabaee yazdi

thankyou for this tools. i have error: Undefined type 'eloquentFilter\QueryFilter\ModelFilters\Filterable'.intelephense(1009)
in laravel 7

Collapse
 
mehdifathi profile image
Mehdi Fathi

Please explain more for help you.

Collapse
 
skeinet profile image
Giorgio

What about apply this filter on joins?
I'va installed mehdi-fathi/eloquent-filter and it works perfectly, but filter doesn't take effect where there is a join

Some idea?

Collapse
 
mehdifathi profile image
Mehdi Fathi

This article is about the old version you should read about this feature at github.com/mehdi-fathi/eloquent-fi....

Collapse
 
saber13812002 profile image
Saber tabatabaee yazdi

your star link github.com/mehdi-fathi/eloquent-fi... in article didnt worked

Collapse
 
mehdifathi profile image
Mehdi Fathi

You are right I've fixed it now.

Collapse
 
saber13812002 profile image
Saber tabatabaee yazdi

i tested the trait as firstname_like and its worked. nice. thank you