在Laravel中使用DB :: raw时会出现奇怪的null

时间:2018-07-09 21:34:55

标签: php laravel

@JsonRpcService(“/abc”)
Public interface ABC {
    Int tmpA_1();
    Int tmpA_2();
    Int tmpB_1();
    Int tmpB_2();
    Int tmpC_1();
    Int tmpC_2();
}

这是我用于查询的代码,问题是它确实返回了一条奇怪的sql语句并释放了sql中的错误,而out put语句如下所示:

        AccountCostCenterUser::Where(function ($query) use ($input){
        $query->Where("account_code",'like','%'.$input.'%')
              ->orWhere("cost_center_code",'like','%'.$input.'%')
              ->orwhere(DB::raw("JSON_VALUE(account_name,'$.".App::getLocale()."') like '%".$input."%'"))
              ->orWhere(DB::raw("JSON_VALUE(cost_center_name,'$.".App::getLocale()."') like '%".$input."%'"));
    })->Where(function($query) use ($user){
        $query->where('account_user','=',$user)
              ->where('cost_center_user','=',$user);
    })->get();

(为什么select * from [v_account_cost_center_valid] where ([account_code] like %400% or [cost_center_code] like %400% or JSON_VALUE(account_name,'$.ar') like '%400%' **is null** or JSON_VALUE(cost_center_name,'$.ar') like '%400%' **is null**) and ([account_user] = 1 and [cost_center_user] = 1) 语句始终出现在我的代码中间,以及如何解决该问题!)

1 个答案:

答案 0 :(得分:2)

替换

->orwhere(DB::raw("JSON_VALUE(account_name,'$.".App::getLocale()."') like '%".$input."%'"))

使用

->orWhereRaw("JSON_VALUE(account_name,'$.".App::getLocale()."') like ?", ['%'.$input.'%']))

您可以在此处了解更多信息:https://laravel.com/docs/5.6/queries#raw-expressions