Laravel 5.2 - 左连接 DB::Raw 不起作用?

Laravel 5.2 - Left Join DB::Raw not working?

我有以下查询,我在其中尝试使用 DB::Raw() 进行左连接,但出现错误:

Missing argument 2 for Illuminate\Database\Query\Builder::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)

您想将原始函数作为第二列传递:

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 建议来自

我不确定 '$.locationId' 是什么,但如果它是一个变量,您可以将其作为数组中的参数作为 DB::raw() 函数的第二个参数传递。