Level 70
Wouldn't this work, if you add inside your map function return $value['user']['name']?
7 likes
I want to map object property user to its name. I'm trying to map it like that, but it's doesn't change anything.
My code for getting results:
$data = $stats
->with('user')
->get()
->map(function ($value, $key) {
$value['user'] = $value['user']['name'];
return $value;
});
Current resulted data:
{
"data": [
{
"total": 4,
"user": {
"id": 3,
"name": "test1"
}
}
]
}
Desired result:
{
"data": [
{
"total": 4,
"user": "test1"
}
]
}
That's what I mean-
$data = $stats
->with('user')
->get()
->map(function ($value, $key) {
return [
'total' => $value['total'],
'user' => $value['user']['name'],
];
});
Please or to participate in this conversation.