From 28fd3b0a5eeb172c7d5003fd67a4456100fdbf63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20N=C3=BCrnberger?= Date: Fri, 6 Nov 2020 10:52:39 +0100 Subject: [PATCH 1/2] Turn the collection into a base collection if mapWithKeys loses models --- .../Database/Eloquent/Collection.php | 17 +++++++++++ .../DatabaseEloquentCollectionTest.php | 29 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index 2bb93f1e1def..d6edc25c933b 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -258,6 +258,23 @@ public function map(callable $callback) }) ? $result->toBase() : $result; } + /** + * Run an associative map over each of the items. + * + * The callback should return an associative array with a single key/value pair. + * + * @param callable $callback + * @return \Illuminate\Support\Collection|static + */ + public function mapWithKeys(callable $callback) + { + $result = parent::mapWithKeys($callback); + + return $result->contains(function ($item) { + return ! $item instanceof Model; + }) ? $result->toBase() : $result; + } + /** * Reload a fresh model instance from the database for all the entities. * diff --git a/tests/Database/DatabaseEloquentCollectionTest.php b/tests/Database/DatabaseEloquentCollectionTest.php index cd3c4e488da0..44669d314688 100755 --- a/tests/Database/DatabaseEloquentCollectionTest.php +++ b/tests/Database/DatabaseEloquentCollectionTest.php @@ -228,6 +228,35 @@ public function testMappingToNonModelsReturnsABaseCollection() $this->assertEquals(BaseCollection::class, get_class($c)); } + public function testMapWithKeys() + { + $one = m::mock(Model::class); + $two = m::mock(Model::class); + + $c = new Collection([$one, $two]); + + $key = 0; + $cAfterMap = $c->mapWithKeys(function ($item) use (&$key) { + return [$key++ => $item]; + }); + + $this->assertEquals($c->all(), $cAfterMap->all()); + $this->assertInstanceOf(Collection::class, $cAfterMap); + } + + public function testMapWithKeysToNonModelsReturnsABaseCollection() + { + $one = m::mock(Model::class); + $two = m::mock(Model::class); + + $key = 0; + $c = (new Collection([$one, $two]))->mapWithKeys(function ($item) use (&$key) { + return [$key++ => 'not-a-model']; + }); + + $this->assertEquals(BaseCollection::class, get_class($c)); + } + public function testCollectionDiffsWithGivenCollection() { $one = m::mock(Model::class); From 90814098afeafee0d69ace0093e8e7130c085975 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 9 Nov 2020 08:16:37 -0600 Subject: [PATCH 2/2] Update Collection.php --- src/Illuminate/Database/Eloquent/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index d6edc25c933b..3737ed59666f 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -261,7 +261,7 @@ public function map(callable $callback) /** * Run an associative map over each of the items. * - * The callback should return an associative array with a single key/value pair. + * The callback should return an associative array with a single key / value pair. * * @param callable $callback * @return \Illuminate\Support\Collection|static