Skip to content

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
mohammad-fouladgar pushed a commit to mohammad-fouladgar/framework that referenced this pull request Jul 22, 2025
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