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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community