Skip to content

Commit

Permalink
Forget with collections (#47637)
Browse files Browse the repository at this point in the history
* Forget with collections

* Types

* Types
  • Loading branch information
joelbutcher authored Jul 4, 2023
1 parent 6543ca6 commit 6b948d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,13 @@ public function flip()
/**
* Remove an item from the collection by key.
*
* @param TKey|array<array-key, TKey> $keys
* \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TKey>|TKey $keys
*
* @return $this
*/
public function forget($keys)
{
foreach ((array) $keys as $key) {
foreach ($this->getArrayableItems($keys) as $key) {
$this->offsetUnset($key);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 6b948d9

Please sign in to comment.