Laravel left join多个字段关联的写法

在Laravel框架中,当两个表关联查询时,如果需要多个字段进行关联的话,可以使用写法如下:
DB::table('table1 as t1')
->join('table2 as t2',function($join){
    $join->on('t1.id1','=','t2.id2')
        ->where('t1.type1','=','t2.type2')
        ->where('t2.status','=','OK');
}, null,null,'left')
->get();

//相当于sql语句如下
SELECT * FROM table1 as t1
LEFT JOIN table2 as t2 
ON t1.id1 = t2.id2 
AND t1.type1 = t2.type2 
AND t2.status = 'OK' 

你可能感兴趣的:(php)