Suppose there are 3 criteria: the keyword $, $ city and $ pricePoint. You can do:
$query = DB::table('users');
if (!empty($keyword)) {
$query = $query->where('keyword', 'LIKE', '%'.$keyword.'%');
}
if (!empty($city)) {
$query = $query->where('city', 'LIKE', '%'.$city.'%');
}
if (!empty($pricePoint)) {
$query = $query->where('pricePoint', 'LIKE', '%'.$pricePoint.'%');
}
$results = $query->get();
Then you can, of course, do something more general by doing a list of criteria and iterating through them using a loop instead of adding other if conditions.
source
share