Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Add a method to map keys #13743

Closed
wants to merge 2 commits into from

Conversation

phroggyy
Copy link
Contributor

@phroggyy phroggyy commented May 27, 2016

This PR introduces mapKeys to the Collection, to allow you to modify keys rather than values. A prime use case for this is in https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Relations/MorphTo.php#L197-L205 where that code could be replaced by

return BaseCollection::make($this->query->getEagerLoads())
    ->filter(function ($constraint, $relation) {
        return Str::startsWith($relation, $this->relation.'.');
    })->mapKeys(function ($key) {
        return Str::replaceFirst($this->relation.'.', '', $key);
    })->merge($instance->getEagerLoads())->all();

In this PR, I also decided to do just that, because why not. This also makes #13741 unnecessary.

@phroggyy
Copy link
Contributor Author

@taylorotwell any chance we can merge this? It just makes things clearer and ensures people don't have to do workarounds to edit keys.

@JosephSilber
Copy link
Member

For the record, this can be achieved with the keyBy method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants