kassir123
2 weeks ago

Use map on Laravel single instance

Posted 2 weeks ago by kassir123

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"
        }
    ]
}

Please sign in or create an account to participate in this conversation.