Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forget with collections #47637

Merged
merged 3 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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