Friday, March 31, 2017

Eloquent join table with two conditions

I have two tables:

A: id | user_id | phonenumber | status | ...

B: id | user_id | phonenumber | name | ...

I would like to list all elements in table A but I have to left join the elements to table B.

I can do it with DB:

DB::table('A')
  ->leftJoin('B', function ($join) {
    $join->on('A.phonenumber', '=', 'B.phonenumber')
         ->on('A.user_id', '=', 'B.user_id');
  })
  ->select(...)
  ->where('A.user_id', '=', $userId)
  ->get();

Is it possible to solve it with eloquent somehow?



via Tamás Mazuk

Advertisement