laravel – Eloquent Complex加入

我做了一个范围

public function scopeCollaborative($query){
    return $query->leftJoin('collaborative', function($join){
        $join->on('imms.phone2', '=', 'collaborative.phone')
            ->orOn('imms.phone', '=', 'collaborative.phone')
            ->where('collaborative.user_id', '=', App('CURUSER')->id);
    });
}

在查询日志中,此范围添加:

left join `cs_collaborative` on 
    `cs_imms`.`phone2` = `cs_collaborative`.`phone` or 
    `cs_imms`.`phone` = `cs_collaborative`.`phone` and 
    `cs_collaborative`.`user_id` = 3

但我需要:

left join `cs_collaborative` on 
    (`cs_imms`.`phone2` = `cs_collaborative`.`phone` or 
    `cs_imms`.`phone` = `cs_collaborative`.`phone`) and 
    `cs_collaborative`.`user_id` = 3

我没有找到一个好的解决方案,JoinClause有功能:On,orOn,where,orWhere.

但并非所有都可以作为输入和分组查询…

有人的理想?

最佳答案 Laravel不允许你构建这样的join子句,所以你需要它来使它工作:

public function scopeCollaborative($query){
    return $query->leftJoin('collaborative', function($join){
        $join->on('imms.phone2', '=', 'collaborative.phone')
            ->where('collaborative.user_id', '=', App('CURUSER')->id)
            ->orOn('imms.phone', '=', 'collaborative.phone')
            ->where('collaborative.user_id', '=', App('CURUSER')->id);
    });
}
点赞