From 5b8f79e9879cc87f5c0ec98c6725608c336a91f1 Mon Sep 17 00:00:00 2001 From: Fa Perreault Date: Mon, 10 Mar 2025 08:29:52 -0400 Subject: [PATCH 1/3] Fix collection value() method to properly handle falsy values --- .../Collections/Traits/EnumeratesValues.php | 2 +- tests/Support/SupportCollectionTest.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Traits/EnumeratesValues.php b/src/Illuminate/Collections/Traits/EnumeratesValues.php index 64736927f1ce..bffd3959224d 100644 --- a/src/Illuminate/Collections/Traits/EnumeratesValues.php +++ b/src/Illuminate/Collections/Traits/EnumeratesValues.php @@ -330,7 +330,7 @@ public function firstWhere($key, $operator = null, $value = null) */ public function value($key, $default = null) { - if ($value = $this->firstWhere($key)) { + if ($value = $this->first()) { return data_get($value, $key, $default); } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 41b5f06f629f..34fad41fe35a 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1144,6 +1144,35 @@ public function testValue($collection) $this->assertEquals(['value' => 'foo'], $c->value('pivot')); $this->assertEquals('foo', $c->value('pivot.value')); $this->assertEquals('bar', $c->where('id', 2)->value('pivot.value')); + + // Test falsy values + $c = new $collection([ + ['id' => 1, 'score' => 0], + ['id' => 2, 'score' => 100], + ]); + + $this->assertSame(0, $c->value('score')); + + $c = new $collection([ + ['id' => 1, 'active' => false], + ['id' => 2, 'active' => true], + ]); + + $this->assertSame(false, $c->value('active')); + + $c = new $collection([ + ['id' => 1, 'code' => '0'], + ['id' => 2, 'code' => '123'], + ]); + + $this->assertSame('0', $c->value('code')); + + $c = new $collection([ + ['id' => 1, 'description' => ''], + ['id' => 2, 'description' => 'Some description'], + ]); + + $this->assertSame('', $c->value('description')); } #[DataProvider('collectionClassProvider')] From c8fcbae6f23be297974659e8f2bc30889630f9fb Mon Sep 17 00:00:00 2001 From: Fa Perreault Date: Mon, 10 Mar 2025 09:41:26 -0400 Subject: [PATCH 2/3] Update src/Illuminate/Collections/Traits/EnumeratesValues.php Co-authored-by: Steve Bauman --- src/Illuminate/Collections/Traits/EnumeratesValues.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Traits/EnumeratesValues.php b/src/Illuminate/Collections/Traits/EnumeratesValues.php index bffd3959224d..2ff0a2e4416e 100644 --- a/src/Illuminate/Collections/Traits/EnumeratesValues.php +++ b/src/Illuminate/Collections/Traits/EnumeratesValues.php @@ -330,7 +330,7 @@ public function firstWhere($key, $operator = null, $value = null) */ public function value($key, $default = null) { - if ($value = $this->first()) { + if ($value = $this->firstWhere($key, '!==', null)) { return data_get($value, $key, $default); } From ee6ff83dbf90a91951ce8b89a6e1d8692e17dd8a Mon Sep 17 00:00:00 2001 From: Fa Perreault Date: Mon, 10 Mar 2025 10:38:45 -0400 Subject: [PATCH 3/3] Add test to make sure items without the key are not returned by the value method --- tests/Support/SupportCollectionTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 34fad41fe35a..bc3e9167c818 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1173,6 +1173,14 @@ public function testValue($collection) ]); $this->assertSame('', $c->value('description')); + + // Skip values that do not have the key + $c = new $collection([ + ['id' => 1], + ['id' => 2, 'description' => ''], + ]); + + $this->assertSame('', $c->value('description')); } #[DataProvider('collectionClassProvider')]