I want to write a search query on Laravel on the basis of either "keyword" or "experience" or "location" search query should run if any of these variable exists.
I am making an Ajax call to achieve this
jobsController.php
public function homepageSearch() {
$_POST = json_decode(file_get_contents('php://input'), true);
$jobs = Jobs::latest('created_at')->search()->get();
echo $jobs;
}
Model jobs.php
class Jobs extends Model {
public function scopeSearch($query) {
$query->where('job_title', 'like', '%'.$_POST['keyword'].'%')
->orWhere('job_description', 'like', '%'.$_POST['keyword'].'%');
if(isset($_POST['keyword'])) {
$query->where('job_title', 'like', '%'.$_POST['keyword'].'%')
->orWhere('job_description', 'like', '%'.$_POST['keyword'].'%');
}
if(isset($_POST['experience'])) {
$query->where('min_exp', $_POST['experience'])
->orWhere('max_exp', $_POST['experience']);
}
if(isset($_POST['city'])) {
$query->where('job_location','like','%'.$_POST['city'].'%');
}
}
}
I want to search on the basis of either keyword or city or experience is this correct way to achieve this in laravel?
I am new to Laravel. Can you suggest me with this.