From a5b6c2a188a835a2431480a2dc477e1d26258d79 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Tue, 6 Mar 2018 22:02:45 +0200 Subject: [PATCH 1/2] fix issue with resources loading assoc marge values --- .../ConditionallyLoadsAttributes.php | 3 ++- tests/Integration/Http/ResourceTest.php | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php b/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php index e6f70a098251..17c99e4c4ba0 100644 --- a/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php +++ b/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php @@ -80,7 +80,8 @@ protected function removeMissingValues($data, $numericKeys = false) } } - return $numericKeys ? array_values($data) : $data; + return ! empty($data) && is_numeric(array_keys($data)[0]) + ? array_values($data) : $data; } /** diff --git a/tests/Integration/Http/ResourceTest.php b/tests/Integration/Http/ResourceTest.php index 5b52520ac61d..14b39f634cef 100644 --- a/tests/Integration/Http/ResourceTest.php +++ b/tests/Integration/Http/ResourceTest.php @@ -524,6 +524,26 @@ public function test_original_on_response_is_collection_of_model_when_collection }); } + public function test_leading_merge__keyed_value_is_merged_correctly() + { + $filter = new class { + use ConditionallyLoadsAttributes; + + public function work() + { + return $this->filter([ + new MergeValue(['name' => 'mohamed', 'location' => 'hurghada']), + ]); + } + }; + + $results = $filter->work(); + + $this->assertEquals([ + 'name' => 'mohamed', 'location' => 'hurghada' + ], $results); + } + public function test_leading_merge_value_is_merged_correctly() { $filter = new class { From ad6db02dd58d4eb2899876b5bc7a5878bfde7d0a Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Tue, 6 Mar 2018 22:04:33 +0200 Subject: [PATCH 2/2] fix style --- tests/Integration/Http/ResourceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Http/ResourceTest.php b/tests/Integration/Http/ResourceTest.php index 14b39f634cef..52988619c9c6 100644 --- a/tests/Integration/Http/ResourceTest.php +++ b/tests/Integration/Http/ResourceTest.php @@ -540,7 +540,7 @@ public function work() $results = $filter->work(); $this->assertEquals([ - 'name' => 'mohamed', 'location' => 'hurghada' + 'name' => 'mohamed', 'location' => 'hurghada', ], $results); }