From ddf6fb018e642a4dea7a1f86032b69555493575c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Pe=C3=B1a?= Date: Mon, 24 Jul 2023 11:50:43 +0200 Subject: [PATCH] Collection::except() with null returns all --- src/Illuminate/Collections/Collection.php | 4 ++++ src/Illuminate/Database/Eloquent/Collection.php | 4 ++++ tests/Database/DatabaseEloquentCollectionTest.php | 3 ++- tests/Support/SupportCollectionTest.php | 4 +++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index a4589992aa29..42d30a55db1e 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -362,6 +362,10 @@ protected function duplicateComparator($strict) */ public function except($keys) { + if (is_null($keys)) { + return new static($this->items); + } + if ($keys instanceof Enumerable) { $keys = $keys->all(); } elseif (! is_array($keys)) { diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index 9a76b88149fd..f18b79c79098 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -475,6 +475,10 @@ public function only($keys) */ public function except($keys) { + if (is_null($keys)) { + return new static($this->items); + } + $dictionary = Arr::except($this->getDictionary(), array_map($this->getDictionaryKey(...), (array) $keys)); return new static(array_values($dictionary)); diff --git a/tests/Database/DatabaseEloquentCollectionTest.php b/tests/Database/DatabaseEloquentCollectionTest.php index 7553e947d0de..1cafbbc3cc83 100755 --- a/tests/Database/DatabaseEloquentCollectionTest.php +++ b/tests/Database/DatabaseEloquentCollectionTest.php @@ -435,13 +435,14 @@ public function testExceptReturnsCollectionWithoutGivenModelKeys() $one->shouldReceive('getKey')->andReturn(1); $two = m::mock(Model::class); - $two->shouldReceive('getKey')->andReturn('2'); + $two->shouldReceive('getKey')->andReturn(2); $three = m::mock(Model::class); $three->shouldReceive('getKey')->andReturn(3); $c = new Collection([$one, $two, $three]); + $this->assertEquals($c, $c->except(null)); $this->assertEquals(new Collection([$one, $three]), $c->except(2)); $this->assertEquals(new Collection([$one]), $c->except([2, 3])); } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index eab6bcee779a..c9253a1cb2d6 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -2264,12 +2264,14 @@ public function testExcept($collection) { $data = new $collection(['first' => 'Taylor', 'last' => 'Otwell', 'email' => 'taylorotwell@gmail.com']); + $this->assertEquals($data->all(), $data->except(null)->all()); $this->assertEquals(['first' => 'Taylor'], $data->except(['last', 'email', 'missing'])->all()); $this->assertEquals(['first' => 'Taylor'], $data->except('last', 'email', 'missing')->all()); - $this->assertEquals(['first' => 'Taylor'], $data->except(collect(['last', 'email', 'missing']))->all()); + $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except(['last'])->all()); $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except('last')->all()); + $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except(collect(['last']))->all()); } /**