Skip to content

Commit 6b948d9

Browse files
authored
Forget with collections (#47637)
* Forget with collections * Types * Types
1 parent 6543ca6 commit 6b948d9

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,13 @@ public function flip()
424424
/**
425425
* Remove an item from the collection by key.
426426
*
427-
* @param TKey|array<array-key, TKey> $keys
427+
* \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TKey>|TKey $keys
428+
*
428429
* @return $this
429430
*/
430431
public function forget($keys)
431432
{
432-
foreach ((array) $keys as $key) {
433+
foreach ($this->getArrayableItems($keys) as $key) {
433434
$this->offsetUnset($key);
434435
}
435436

tests/Support/SupportCollectionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,21 @@ public function testForgetArrayOfKeys()
798798
$this->assertTrue(isset($c['name']));
799799
}
800800

801+
public function testForgetCollectionOfKeys()
802+
{
803+
$c = new Collection(['foo', 'bar', 'baz']);
804+
$c = $c->forget(collect([0, 2]))->all();
805+
$this->assertFalse(isset($c[0]));
806+
$this->assertFalse(isset($c[2]));
807+
$this->assertTrue(isset($c[1]));
808+
809+
$c = new Collection(['name' => 'taylor', 'foo' => 'bar', 'baz' => 'qux']);
810+
$c = $c->forget(collect(['foo', 'baz']))->all();
811+
$this->assertFalse(isset($c['foo']));
812+
$this->assertFalse(isset($c['baz']));
813+
$this->assertTrue(isset($c['name']));
814+
}
815+
801816
/**
802817
* @dataProvider collectionClassProvider
803818
*/

0 commit comments

Comments
 (0)