diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 6a9e96f4ed54..ae772b70cbac 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1041,6 +1041,17 @@ public function split($numberOfGroups) return $groups; } + /** + * Split a collection into a certain number of groups, and fill the first groups completely. + * + * @param int $numberOfGroups + * @return static + */ + public function splitIn($numberOfGroups) + { + return $this->chunk(ceil($this->count() / $numberOfGroups)); + } + /** * Chunk the collection into chunks of the given size. * diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 55b8d97d8540..2384948f951a 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1057,6 +1057,17 @@ public function chunk($size) }); } + /** + * Split a collection into a certain number of groups, and fill the first groups completely. + * + * @param int $numberOfGroups + * @return static + */ + public function splitIn($numberOfGroups) + { + return $this->chunk(ceil($this->count() / $numberOfGroups)); + } + /** * Chunk the collection into chunks with a callback. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 851fe930d474..6faf65a1a3c2 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1645,6 +1645,22 @@ public function testChunkWhenGivenLessThanZero($collection) ); } + /** + * @dataProvider collectionClassProvider + */ + public function testSplitIn($collection) + { + $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + $data = $data->splitIn(3); + + $this->assertInstanceOf($collection, $data); + $this->assertInstanceOf($collection, $data->first()); + $this->assertCount(3, $data); + $this->assertEquals([1, 2, 3, 4], $data->get(0)->values()->toArray()); + $this->assertEquals([5, 6, 7, 8], $data->get(1)->values()->toArray()); + $this->assertEquals([9, 10], $data->get(2)->values()->toArray()); + } + /** * @dataProvider collectionClassProvider */