I would like to have a custom json response from the results of eloquent. SO basically I have a table that list all the fields like this:
userid
name
address1
address2
state
city
zip
Then using a query like this:
$results = User::paginate(10);
Then I would like to have a custom response from the $results like this:
[
{
"id":1,
"name": "John Doe",
"AddressInfo" {
"address1" : "some address1 here...",
"address2" : "some address2 here...",
"state" : "TX",
"city" : "San Antonio",
"zip" : "12345"
}
},
{
//other records...
}
]
My current solutions is to do the foreach()
foreach($result as $k=>$v){
$arrItem = [
'id' => $v->userid,
'AddressInfo' => [ ... ]
]
}
Code above is working but thinking using different approach and optimization like map() in laravel.
Anyone knows how to achieve the output.
Thanks