Full outer join laravel

Full Outer Join in Laravel

In Laravel, a full outer join can be performed using the leftJoin() and union() methods provided by the query builder.

Example:

$result = DB::table('table1')
                ->leftJoin('table2', 'table1.column', '=', 'table2.column')
                ->select('table1.column', 'table2.column')
                ->union(DB::table('table1')
                         ->rightJoin('table2', 'table1.column', '=', 'table2.column')
                         ->select('table1.column', 'table2.column'))
                ->get();

In the above example, table1 and table2 are the tables for which we want to perform a full outer join. The leftJoin() method is used to perform a left join and the rightJoin() method is used to perform a right join.

The leftJoin() method selects the columns from table1 and table2 where the join condition is met. The rightJoin() method selects the columns from table1 and table2 where the join condition is met in the opposite way (right join).

The union() method is used to combine the results of both the left join and right join queries. The select() method is used to specify the columns to be selected from both tables.

The get() method retrieves the resulting rows from the query.

Finally, the $result variable contains the rows resulting from the full outer join operation.

Read more interesting post

Leave a comment