I have the problem when I pass empty parameters in a filter. I am using a scope where I filter by several parameters, including year, month, province, and name.
About Name there are no problems since I use a like. But in others when filter, the parameters are optional. I can filter by all or by some. But here the question when I filter by some and leave another vacuum. this where looking for a vacuum and can not find it, makes the query empty.
The first solution I can think of is a cumbersome if nested with the 9 options (since they are 3 empty possibilities) and putting in each 1 the where are not empty ... I want to believe that there is a better solution
public function scopeSearch($query, $request)
{
if($request->nombre == "" && $request->provincia_id == "" && $request->meses =="" && $request->anio == "")
return $query
->where('fecha', '>=', Carbon::now());
return $query
->where('nombre','LIKE',"%$request->nombre%")
->whereMonth('fecha', $request->meses)
->whereYear('fecha', $request->anio);
//->where('provincia_id','=',"$request->provincia_id");
}