Support the ongoing development of Laravel.io →
Artisan Laravel Laravel.io
0

Hi @gauravs456 ,

The better way to get your expected output is to use joins. The example of the same is given below:

Eloquent Query

     $result = User::select(
                            "users.id", 
                            "users.name",
                            "order.name as order_name"
                        )
                        ->leftJoin("orders", "orders.userid", "=", "users.id")
                        ->get();

SQL Query

select `users`.`id`, `users`.`name`, `orders`.`name` as `order_name` 

	from `users` 

	left join `orders` on `orders`.`userid` = `users`.`id`;

Hope this helps you in getting your expected output.

0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.