Skip to content
Advertisement

Add “where” and “whereIn” clause only when filter value is not null

I am looking for optimum way to add where clause in Laravel query when there are multiple filters(5+ filters) only if filter values are not null.

Right now I am having 6 filters on report on front end which will send input to api.

below is a pseudocode which i want to achive.

JavaScript

I know the simple solution using combination of multiple if else conditions. but i want to know if there is any other optimum way to do the solution.

Advertisement

Answer

use when() method

Here is the definition

JavaScript

I refactored your code a little

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