From 7a656ce5ee28762319845e64b8458c80bbefc0f3 Mon Sep 17 00:00:00 2001 From: Joseph Silber Date: Sun, 29 May 2016 20:59:43 -0400 Subject: [PATCH] Pass the key to the keyBy callback --- src/Illuminate/Support/Collection.php | 4 ++-- tests/Support/SupportCollectionTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index c3cb4b3739b7..5aa615675fee 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -374,8 +374,8 @@ public function keyBy($keyBy) $results = []; - foreach ($this->items as $item) { - $results[$keyBy($item)] = $item; + foreach ($this->items as $key => $item) { + $results[$keyBy($item, $key)] = $item; } return new static($results); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 821211b7167c..870a817bd0bf 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -961,12 +961,12 @@ public function testKeyByClosure() ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'], ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'], ]); - $result = $data->keyBy(function ($item) { - return strtolower($item['firstname'].$item['lastname']); + $result = $data->keyBy(function ($item, $key) { + return strtolower($key.'-'.$item['firstname'].$item['lastname']); }); $this->assertEquals([ - 'taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'], - 'lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'], + '0-taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'], + '1-lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'], ], $result->all()); }