基于la变量中是否存在post变量的搜索查询

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.

class Job extends Model {
    public function scopeSearch($query, $keyword, $experience, $city) {
        $query->where(function ($q) {
            $q->where('job_title', 'like', '%'.$_POST['keyword'].'%')
              ->orWhere('job_description', 'like', '%'.$_POST['keyword'].'%');
        });

        if(isset($keyword)) {
            $query->where(function ($q) {
                $q->where('job_title', 'like', '%'.$_POST['keyword'].'%')
                  ->orWhere('job_description', 'like', '%'.$_POST['keyword'].'%');
            });
        }

        if(isset($experience)) {
            $query->where(function($q) {
                $q->where('min_exp', $_POST['experience'])
                  ->orWhere('max_exp', $_POST['experience']);   
            });
        }

        if(isset($city)) {
            $query->where('job_location','like','%'.$_POST['city'].'%');
        }

    return $query;
    }
}

Call from your controller using the following:

Job::search($request->input('keyword'), 
$request->input('experience'), $request->input('city'));

A few observations/suggestions:

  1. Where chaining needs to be correct. When you say $query->where(..a..)->orWhere(..b..)->where(..c..)->orWhere(..d..) it will evaluate to: ((a && c) || b || d). Where you intended ((a || b) && (c || d)). This is why you need to use closures like I have above using parameter grouping
  2. Avoid using $_POST, use the Request object instead as Laravel does quite a lot of work for you when you use $request
  3. Avoid calling your request object from the model. It's not the model's responsibility to check request/post variables, it's your controller's responsibility to do so. Use dynamic scopes instead to segregate the responsibilities
  4. You need to return the query object in scopes
  5. A model is one entity. So "a job" is a model not "jobs". So I renamed the Jobs class to Job :)