8000 [12.x] Add `Closure`-support to `$key`/`$value` in Collection `pluck()` method by ralphjsmit · Pull Request #56188 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add Closure-support to $key/$value in Collection pluck() method #56188

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

Merged
merged 2 commits into from
Jul 1, 2025

Conversation

ralphjsmit
Copy link
Contributor

This PR adds closure support to the $key/$value parameters of the pluck() methods. Very often I have that I almost can use pluck(), but I cannot because I need to make a slight modification to e.g. the key or value. I then need to solve this with mapWithKeys(), but that repeats the id and makes it way more verbose if you only just want to apply some formatting to the key and/or value.

After this PR, you can now do the following:

Country::get()
    ->pluck(fn (Country $country) => "{$country->flag} {$country->name}", 'id')

This would result in a collection keyed by id with the concatenated name as value. Eloquent collections are a common use case, but this also works on regular collections and the Arr class.

Thanks!

@taylorotwell taylorotwell merged commit 74811d4 into laravel:12.x Jul 1, 2025
39 of 60 checks passed
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.

2 participants
0