[英]Eloquent Join - where closure
Is there any way to use closure for where
in join clause in Eloquent? 有什么方法可以对Eloquent中的join子句中的
where
使用闭包?
What I'm trying to do is: 我想做的是:
$model = $model->leftJoin('history', function ($join) {
$join->on('history.record_id', '=', 'work_order.work_order_id');
$join->where('history.tablename', '=', 'test');
$join->where('history.columnname', '=', 'column');
$join->where(function ($q) {
$q->where('history.value_from', '=', '0')
->orWhere('history.value_from', '=', '');
});
$join->where('history.value_to', '>', '0');
});
but obviously the part: 但很明显的一部分:
$join->where(function ($q) {
$q->where('history.value_from', '=', '0')
->orWhere('history.value_from', '=', '');
});
doesn't work because JoinClause
doesn't support closure for where 不起作用,因为
JoinClause
不支持在何处关闭
The method you are looking for is called whereNested
and is available from the Illumintate\\Database\\Query\\Builder
class. 您要查找的方法称为
whereNested
,可从Illumintate\\Database\\Query\\Builder
类获得。
Unfortunately, the $join
parameter passed to the join closure is of type Illumintate\\Database\\Query\\JoinClause
which only has 4 methods for handling where statements for the join where
, orWhere
, whereNull
and whereNotNull
. 不幸的是,传递给
Illumintate\\Database\\Query\\JoinClause
闭包的$join
参数的类型为Illumintate\\Database\\Query\\JoinClause
,它只有4种方法来处理其中where
, orWhere
, whereNull
和whereNotNull
的join语句。
To make this work you'll need to resort to using DB::raw
. 为了完成这项工作,您将需要使用
DB::raw
。 This should work: 这应该工作:
$model = $model->leftJoin('history', function ($join) {
$join->on('history.record_id', '=', 'work_order.work_order_id');
$join->where('history.tablename', '=', 'test');
$join->where('history.columnname', '=', 'column');
$join->where(DB::raw('(`history`.`value_from` = 0 or `history`.`value_from` = "")'), '', '');
$join->where('history.value_to', '>', '0');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.