Skip to content

Commit 98aa74a

Browse files
mishavantolMisha van Tol
andauthored
fix nth where step <= offset (#41645)
Co-authored-by: Misha van Tol <[email protected]>
1 parent 447f620 commit 98aa74a

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,8 @@ public function nth($step, $offset = 0)
806806

807807
$position = 0;
808808

809-
foreach ($this->items as $item) {
810-
if ($position % $step === $offset) {
809+
foreach ($this->slice($offset)->items as $item) {
810+
if ($position % $step === 0) {
811811
$new[] = $item;
812812
}
813813

src/Illuminate/Collections/LazyCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,8 @@ public function nth($step, $offset = 0)
796796
return new static(function () use ($step, $offset) {
797797
$position = 0;
798798

799-
foreach ($this as $item) {
800-
if ($position % $step === $offset) {
799+
foreach ($this->slice($offset) as $item) {
800+
if ($position % $step === 0) {
801801
yield $item;
802802
}
803803

tests/Support/SupportCollectionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2956,6 +2956,8 @@ public function testNth($collection)
29562956
$this->assertEquals(['b', 'f'], $data->nth(4, 1)->all());
29572957
$this->assertEquals(['c'], $data->nth(4, 2)->all());
29582958
$this->assertEquals(['d'], $data->nth(4, 3)->all());
2959+
$this->assertEquals(['c', 'e'], $data->nth(2, 2)->all());
2960+
$this->assertEquals(['c', 'd', 'e', 'f'], $data->nth(1, 2)->all());
29592961
}
29602962

29612963
/**

0 commit comments

Comments
 (0)