Skip to content

Commit 3cd20ad

Browse files
sebastiandedeynetaylorotwell
authored andcommitted
Fix eachSpread and mapSpread with nested collections (#20962)
1 parent 61ac47b commit 3cd20ad

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Illuminate/Support/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public function each(callable $callback)
352352
public function eachSpread(callable $callback)
353353
{
354354
return $this->each(function ($chunk, $key) use ($callback) {
355-
array_push($chunk, $key);
355+
$chunk[] = $key;
356356

357357
return $callback(...$chunk);
358358
});
@@ -844,7 +844,7 @@ public function map(callable $callback)
844844
public function mapSpread(callable $callback)
845845
{
846846
return $this->map(function ($chunk, $key) use ($callback) {
847-
array_push($chunk, $key);
847+
$chunk[] = $key;
848848

849849
return $callback(...$chunk);
850850
});

tests/Support/SupportCollectionTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,13 @@ public function testEachSpread()
606606
$result[] = [$number, $character, $key];
607607
});
608608
$this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);
609+
610+
$c = new Collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
611+
$result = [];
612+
$c->eachSpread(function ($number, $character, $key) use (&$result) {
613+
$result[] = [$number, $character, $key];
614+
});
615+
$this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);
609616
}
610617

611618
public function testIntersectNull()
@@ -1225,6 +1232,12 @@ public function testMapSpread()
12251232
return "{$number}-{$character}-{$key}";
12261233
});
12271234
$this->assertEquals(['1-a-0', '2-b-1'], $result->all());
1235+
1236+
$c = new Collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
1237+
$result = $c->mapSpread(function ($number, $character, $key) use (&$result) {
1238+
return "{$number}-{$character}-{$key}";
1239+
});
1240+
$this->assertEquals(['1-a-0', '2-b-1'], $result->all());
12281241
}
12291242

12301243
public function testFlatMap()

0 commit comments

Comments
 (0)