Bvc...
Bvc...

Reputation: 191

Laravel Join on Null

As the title says, how do I join on a null condition in Laravel? Take the following query:

SELECT * FROM TABLE1 a 
 INNER JOIN TABLE2 b
  ON a.col1 = b.col1 
     OR a.col2 IS NULL

I've tried this but not the result I wanted:

$query = Table1::join('table2', function($join){
    $join->on('table1.col1', '=', 'table2.col1');
    $join->on('table1.col2', ????????);
});

Thanks.

Upvotes: 3

Views: 1071

Answers (1)

OMR
OMR

Reputation: 12188

you can use where inside JoinClause class, see advanced join clause:

$query = Table1::join('table2', function($join){
    $join->on('table1.col1', '=', 'table2.col1')
    ->orWhereNull('table1.col2');
});

Upvotes: 3

Related Questions