From 6bdd974b75d234ecd8ee86b272321e58bd65806b Mon Sep 17 00:00:00 2001 From: Ahmed Alaa <92916738+AhmedAlaa4611@users.noreply.github.com> Date: Mon, 14 Jul 2025 20:53:18 +0300 Subject: [PATCH] Add closure support to pluck() method in collections --- collections.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/collections.md b/collections.md index be8b7f7259f..024a2175b52 100644 --- a/collections.md +++ b/collections.md @@ -2251,6 +2251,28 @@ $plucked->all(); // ['Tesla' => 'black', 'Pagani' => 'orange'] ``` +The `pluck` method also supports Closure for retrieving values: + +```php +$languages = [ + ['id' => 1, 'code' => 'JS', 'name' => 'JavaScript'], + ['id' => 2, 'code' => 'TS', 'name' => 'TypeScript'], + ['id' => 3, 'code' => 'PY', 'name' => 'Python'], +]; + +$plucked = collect($languages)->pluck( + fn ($language) => "{$language['code']}: {$language['name']}", 'id' +); + +$plucked->all(); + +// [ +// 1 => 'JS: JavaScript', +// 2 => 'TS: TypeScript', +// 3 => 'PY: Python', +// ] +``` + #### `pop()` {.collection-method}