Sunday, October 18, 2020

Laravel search query example

 return $query->when(!empty(request()->input('location', 0)), function($query) {

$query->whereHas('location', function($query) { $query->whereId(request()->input('location')); }); }) ->when(!empty(request()->input('category', 0)), function($query) { $query->whereHas('categories', function($query) { $query->whereId(request()->input('category')); }); }) ->when(!empty(request()->input('search', '')), function($query) { $query->where(function($query) { $search = request()->input('search'); $query->where('title', 'LIKE', "%$search%") ->orWhere('short_description', 'LIKE', "%$search%") ->orWhere('full_description', 'LIKE', "%$search%") ->orWhere('job_nature', 'LIKE', "%$search%") ->orWhere('requirements', 'LIKE', "%$search%") ->orWhere('address', 'LIKE', "%$search%") ->orWhereHas('company', function($query) use($search) { $query->where('name', 'LIKE', "%$search%"); }); }); });

No comments:

Post a Comment