A resplendent complex

I made a region

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);
    });
}

      

in the query log, this scope adds:

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

      

but i need:

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

      

I didn't find a good solution, the JoinClause function has functions: On, orOn, where, or Where.

but not everyone can take a function as input and group the query ...

someone's ideals?

+3


source to share


2 answers


Laravel does not allow creating such a join clause, so you need to do this for it to work:



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);
    });
}

      

+4


source


you might consider using the Doctrine ORM more powerful, less lightweight in the beginning ...



-2


source







All Articles