Using collection method map() in another way

map() method is used to iterate all collection items and helps to make changes to them

Suppose, we need to change user full name into single name taking first name only.

For this, code snippet will be like below.

$users = $users->map( function ($user) {
    $name = explode(' ', $user->name);
    $user->name = $name[0];
    return $user;
});

Related Posts


Using collection method reduce()