简体   繁体   English

雄辩的加入-在哪里关闭

[英]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种方法来处理其中whereorWherewhereNullwhereNotNull的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM