I have multiple dropdown search filter. I can filter all the column of my table if only I select every dropdown. If I'm only selecting one of them or two, it doesn't return any result. Is there anything to add or did I do it wrongly?
Eloquent
$query = Table::when($id, function($q) use ($id){
return $q->where('id', $id);
})
->when($state, function($q) use ($state){
return $q->where('state', $state);
})
->when($country, function($q) use ($country){
return $q->where('country', $country);
})
->when($name, function($q) use ($name){
return $q->where('name', $name);
})
->when($city, function($q) use ($city){
return $q->where('city', $city);
})
->when($status, function($q) use ($status){
return $q->where('status', $status);
})
->paginate(10);
return $query;
Controller
if ($request->id || $request->state || $request->country|| $request->name || $request->city || $request->name || $request->status){
$filter = $eloquent->getTable($request->id, $request->state, //all request);
}
else{
$filter = $eloquent->all();
}
Or is there any other way that can be done? Guide and help will be appreciated.
toSql()instead ofpaginateto see the query.