php - Laravel nested groupBy is returning an unchanged array

I have a collection of objects that look like the following:

user: {
  name: userName,
  region: userRegion,
  role: userRole
}

Each of these objects have already been grouped according to their regions, however I would like to further group them (within the region collections) based on their role.

I have tried doing this:

$Levels = $Regions->each(function ($item, $key) {
    return $item->groupBy(function ($item, $key) {
        return $item['Owner']['role'];
    });
});

However it returns the original, region-grouped collections, which looks like this:

EU => collection {
    #items: array: 10 [
        0 => array: 3,
        1 => array: 3...
    ]
}

Ideally I'd end up with a result like:

EU {
    role1: array,
    role2: array...
}

Any direction would be appreciated.

Answer

Solution:

I fixed this problem by changing the each method to map method.

Source