diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 207022e8b3f4..7c3b72f29cae 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -831,6 +831,23 @@ public function mergeRecursive($items) return new static(array_merge_recursive($this->items, $this->getArrayableItems($items))); } + /** + * Multiply the items in the collection by the multiplier. + * + * @param int $multiplier + * @return static + */ + public function multiply(int $multiplier) + { + $new = new static; + + for ($i = 0; $i < $multiplier; $i++) { + $new->push(...$this->items); + } + + return $new; + } + /** * Create a collection by using this collection for keys and another for its values. * diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 958daf8a7508..374c300021f6 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -841,6 +841,17 @@ public function mergeRecursive($items) return $this->passthru('mergeRecursive', func_get_args()); } + /** + * Multiply the items in the collection by the multiplier. + * + * @param int $multiplier + * @return static + */ + public function multiply(int $multiplier) + { + return $this->passthru('multiply', func_get_args()); + } + /** * Create a collection by using this collection for keys and another for its values. * diff --git a/tests/Database/DatabaseEloquentCollectionTest.php b/tests/Database/DatabaseEloquentCollectionTest.php index 1cafbbc3cc83..d5e9d64652d3 100755 --- a/tests/Database/DatabaseEloquentCollectionTest.php +++ b/tests/Database/DatabaseEloquentCollectionTest.php @@ -510,6 +510,21 @@ public function testMakeVisibleRemovesHiddenAndIncludesVisible() $this->assertEquals(['visible', 'hidden'], $c[0]->getVisible()); } + public function testMultiply() + { + $a = new TestEloquentCollectionModel(); + $b = new TestEloquentCollectionModel(); + + $c = new Collection([$a, $b]); + + $this->assertEquals([], $c->multiply(-1)->all()); + $this->assertEquals([], $c->multiply(0)->all()); + + $this->assertEquals([$a, $b], $c->multiply(1)->all()); + + $this->assertEquals([$a, $b, $a, $b, $a, $b], $c->multiply(3)->all()); + } + public function testQueueableCollectionImplementation() { $c = new Collection([new TestEloquentCollectionModel, new TestEloquentCollectionModel]); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index c345699818ad..f8b0b9d02aa6 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1259,6 +1259,25 @@ public function testMergeRecursiveCollection($collection) ); } + #[DataProvider('collectionClassProvider')] + public function testMultiplyCollection($collection) + { + $c = new $collection(['Hello', 1, ['tags' => ['a', 'b'], 'admin']]); + + $this->assertEquals([], $c->multiply(-1)->all()); + $this->assertEquals([], $c->multiply(0)->all()); + + $this->assertEquals( + ['Hello', 1, ['tags' => ['a', 'b'], 'admin']], + $c->multiply(1)->all() + ); + + $this->assertEquals( + ['Hello', 1, ['tags' => ['a', 'b'], 'admin'], 'Hello', 1, ['tags' => ['a', 'b'], 'admin'], 'Hello', 1, ['tags' => ['a', 'b'], 'admin']], + $c->multiply(3)->all() + ); + } + #[DataProvider('collectionClassProvider')] public function testReplaceNull($collection) {