本文介紹了Laravel 5.2 - 左加入 DB::Raw 不起作用?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有以下查詢,我嘗試使用 DB::Raw()
進行左連接,但出現錯誤:
I have the following query where I'm trying to use DB::Raw()
for the left join but I'm getting error:
IlluminateDatabaseQueryBuilder::leftJoin() 缺少參數 2
Missing argument 2 for IlluminateDatabaseQueryBuilder::leftJoin()
這是我的查詢:
return $this->model->from('alerts as a')
->leftJoin(DB::Raw("locations as l on l.id = JSON_UNQUOTE(JSON_EXTRACT(a.criteria, '$.locationId'))"))
->leftJoin(DB::Raw("industries as i on find_in_set(i.id, JSON_UNQUOTE(JSON_EXTRACT(a.criteria, '$.industries')))"))
->where('user_id', '=', $userId)
->selectRaw("a.id
, a.name
, a.criteria
, GROUP_CONCAT(DISTINCT(i.name) SEPARATOR ', ') as 'Industries'
->groupBy('a.id')
->orderBy('a.created_at', 'desc');
推薦答案
leftJoin
函數聲明如下:
public function leftJoin($table, $first, $operator = null, $second = null)
您想將原始函數作為第二列傳入:
You want to pass your raw functions in as the second column:
return $this->model->from('alerts as a')
->leftJoin('locations AS l', 'l.id', '=', DB::Raw("JSON_UNQUOTE(JSON_EXTRACT(a.criteria, '$.locationId'))"))
->leftJoin('industries as i', function($join){
$join->on(DB::raw("find_in_set(i.id, JSON_UNQUOTE(JSON_EXTRACT(a.criteria, '$.industries')))",DB::raw(''),DB::raw('')));
})
->where('user_id', '=', $userId)
->selectRaw("a.id
, a.name
, a.criteria
, GROUP_CONCAT(DISTINCT(i.name) SEPARATOR ', ') as 'Industries'")
->groupBy('a.id')
->orderBy('a.created_at', 'desc');
find_in_set 建議來自這里.
The find_in_set suggestion came from here.
我不確定 '$.locationId'
是什么,但如果它是一個變量,你可以將它作為一個數組中的參數作為 DB 上的第二個參數傳遞::raw()
函數.
I'm not sure what '$.locationId'
is, but if it's a variable, you can pass that along as a parameter within an array as the second parameter on the DB::raw()
function.
這篇關于Laravel 5.2 - 左加入 DB::Raw 不起作用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!