Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent Complex Joins

i have made a scope

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 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 to have:

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 found a good solution, JoinClause have functions: On, orOn, where, orWhere.

but non of all can take function as input and to group query...

someone ideals?

like image 835
Maximilian Prepl Avatar asked Oct 21 '22 01:10

Maximilian Prepl


1 Answers

Laravel doesn't let you build such join clause, so you need this to make it 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);
    });
}
like image 127
Jarek Tkaczyk Avatar answered Oct 24 '22 10:10

Jarek Tkaczyk