I am having a really tough time figuring out another approach to do search querys on my site. At the moment i have search critierias for "Min size, Max size, City, street, Min. price, Max. price.
So i have these queries (build using Laravel standard):
if (!empty($by) && empty($adresse) && empty($minstor) && empty($maxstor))
{
$users = User::where('pris', '<', $maxpris)->where('pris', '>', $minpris)->where('by', '=', $by)->orderBy('created_at', 'DESC')->get();
}
elseif (!empty($by) && !empty($adresse) && empty($minstor) && empty($maxstor))
{
$users = User::where('pris', '<', $maxpris)->where('pris', '>', $minpris)->where('by', '=', $by)->where('adresse', '=', $adresse)->orderBy('created_at', 'DESC')->get();
}
elseif (empty($by) && !empty($adresse) empty($minstor) && empty($maxstor))
{
$users = User::where('pris', '<', $maxpris)->where('pris', '>', $minpris)->where('adresse', '=', $adresse)->orderBy('created_at', 'DESC')->get();
}
else
{
$users = User::where('pris', '<', $maxpris)->where('pris', '>', $minpris)->orderBy('created_at', 'DESC')->get();
}
All of this simply for the user being able to search for the for the critirias above, but if i want to add several more feautures, i can just imagine how many different if functions
i will need to run in order to get all the specific inputs etc. It will evovle eksponentially really quick.
How can i get around this problem without having to create hundreds of different if else
statements in order to return the correct query?