doubu8643 2017-01-16 13:19 采纳率: 100%
浏览 25
已采纳

基于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.

  • 写回答

1条回答 默认 最新

  • dongqun9403 2017-01-16 14:19
    关注
    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 :)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥15 对于这个问题的算法代码
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题