onlinecode

How to use Union query with Laravel Eloquent?

How to use Union query with Laravel Eloquent?

In this post we will give you information about How to use Union query with Laravel Eloquent?. Hear we will give you detail about How to use Union query with Laravel Eloquent?And how to use it also give you demo for it if it is necessary.

In this post, i would like to share with you how we can use union and union all query in laravel 5 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:

product_silver table:

product_gold table:

Query:

Also see:Laravel – whereIn and whereNotIn with subquery example using query builder

$silver = DB::table("product_silver")

->select("product_silver.name"

,"product_silver.price"

,"product_silver.quantity");

$gold = DB::table("product_gold")

->select("product_gold.name"

,"product_gold.price"

,"product_gold.quantity")

->union($silver)

->get();

dd($gold);

Output:

I hope it can help you….

Hope this code and post will helped you for implement How to use Union query with Laravel Eloquent?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version