简体   繁体   中英

How to do a full join in Laravel Eloquent?

给定两个表 t1 和 t2 在 laravel eloquent 中,如何执行完全连接?

If you were using MySQL.

MySQL has no inbuilt support for full outer join.

But you can use the code like this below to achieve that.

$table2 = DB::table('t2')
         ->rightJoin('t1', 't1.id', '=', 't2.t1_id')

$table1 = DB::table('t1')
        ->leftJoin('t2', 't1.id', '=', 't2.t1_id')
        ->unionAll($table1)
        ->get();

Most of query builder join functions have an optional argument called $type ='inner'
so if you database supports full join (eg: postgres) just pass "full" as the $type parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM