How to use Union Query with Laravel Eloquent?
In this post, i would like to share with you how we can use union and union all query in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 application.
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. So here i am going to give you very simple example with tables and also show you output of result.
Here we will create two tables "product_silver" and "product_gold" with dummy data like as bellow. Then after we will write union query and result also i shown as screenshot, so let's see bellow:
$silver = DB::table("product_silver")
$gold = DB::table("product_gold")
I hope it can help you....
We are Recommending you
- Laravel Eloquent updateOrCreate Example
- Delete All Records from Table in Laravel Eloquent
- Multiple orWhere Condition in Laravel Eloquent
- Laravel Eloquent exists() and doesntExist() Example
- Laravel Eloquent Where Query Examples
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel WhereIn() and WhereNotIn() with Subquery Example
- Laravel - How to use Subquery in Select Statement?
- Laravel Select with Count Query with Group By Example
- Laravel Select with Sum Query Example
- How to Concat Two Columns in Laravel?
- Example of unionAll in Query Builder Laravel
- Laravel Join with Subquery in Query Builder Example
- How to Add Pagination with Union in Laravel?