diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 4fb40056282e..a4589992aa29 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -424,12 +424,13 @@ public function flip() /** * Remove an item from the collection by key. * - * @param TKey|array $keys + * \Illuminate\Contracts\Support\Arrayable|iterable|TKey $keys + * * @return $this */ public function forget($keys) { - foreach ((array) $keys as $key) { + foreach ($this->getArrayableItems($keys) as $key) { $this->offsetUnset($key); } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 04d917d989e3..eab6bcee779a 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -798,6 +798,21 @@ public function testForgetArrayOfKeys() $this->assertTrue(isset($c['name'])); } + public function testForgetCollectionOfKeys() + { + $c = new Collection(['foo', 'bar', 'baz']); + $c = $c->forget(collect([0, 2]))->all(); + $this->assertFalse(isset($c[0])); + $this->assertFalse(isset($c[2])); + $this->assertTrue(isset($c[1])); + + $c = new Collection(['name' => 'taylor', 'foo' => 'bar', 'baz' => 'qux']); + $c = $c->forget(collect(['foo', 'baz']))->all(); + $this->assertFalse(isset($c['foo'])); + $this->assertFalse(isset($c['baz'])); + $this->assertTrue(isset($c['name'])); + } + /** * @dataProvider collectionClassProvider */