search_query.php

<?php // search query from scope in laravel model public function scopeSearchResults($query) { 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%"); }); }); }); }
Search query #laravel #scope

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.