Created
March 26, 2021 08:35
-
-
Save yavgel85/0c28d4eeb185547d478a8eaeffcb53d5 to your computer and use it in GitHub Desktop.
Search query #laravel #scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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%"); | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment