Skip to content
Advertisement

Laravel 6 query related to searching events not showing result when some fields are empty and some not

i’m kind of new to laravel first time developing on it. My goal is to get some filtered events to show, the filters should all work together (‘filter 1’ AND ‘filter2’ ecc..) even when some are empty. The problem that i’m getting is that when my filter fields are empty the query won’t return any event. My CONTROLLER code:

JavaScript

More in detail

JavaScript

My view CatalogoEventi:

JavaScript

My routes

JavaScript

result from the query abovewith field ‘regione’ not empty and others empty

If i only work with this

JavaScript

actually i get some results and i can display them (with some problems referring to the paginator but it’s not the main problem). I think that i should fix the query (and i want to do this way) to get all done But for now i got no clue.

Advertisement

Answer

Your original problem is probably that the input values are not null, but rather empty strings.

But conditional queries like this are a good use of the when() query builder method. If the first argument evaluates to a non-false value, the second argument is executed.

JavaScript

Or, if you are still on old PHP without arrow functions:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement