Laravel eloquent Union and Union all

This will show you how we can use union and union all queries in laravel 6 app.

Laravel eloquent provide query builder and they give us join, relationship, subquery and also union. But we need some time to get all records from two different table at that time you need to use union or union all query.

Here we will create two tables “table_a” and “table_b”

$results_a = DB::table("table_a")
   ->select("table_a.name"
     ,"table_a.price"
     ,"table_a.quantity");
 $results_b = DB::table("table_b")
   ->select("table_b.name"
     ,"table_b.price"
     ,"table_b.quantity")
   ->union($results_a )
   ->get();
 dd($results_b );

Leave a Reply

Close Menu