Laravel 5.2 - Left Join DB::Raw not working?(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 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 5.2 - 左加入 DB::Raw 不起作用?
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
