5

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?

Maximilian Prepl
  • 402
  • 4
  • 10

2 Answers2

8

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);
    });
}
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
-5

you can consider to use Doctrine ORM is more powerfull, bur less easy in the beginning...

gabrielem
  • 560
  • 5
  • 13