Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Join on Null

Tags:

sql

mysql

laravel

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.

like image 743
Bvc... Avatar asked Mar 22 '26 04:03

Bvc...


1 Answers

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');
});
like image 154
OMR Avatar answered Mar 23 '26 18:03

OMR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!