{"record":{"id":"d96e0efa7f1975be","repo":"laravel/framework","slug":"number-of-groups-must-be-at-least-1","errorCode":null,"errorMessage":"Number of groups must be at least 1.","messagePattern":"Number of groups must be at least 1\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/Collection.php","lineNumber":1385,"sourceCode":"     * @return static\n     */\n    public function slice($offset, $length = null)\n    {\n        return $this->newInstance(array_slice($this->items, $offset, $length, true));\n    }\n\n    /**\n     * Split a collection into a certain number of groups.\n     *\n     * @param  int  $numberOfGroups\n     * @return ($numberOfGroups is positive-int ? static<int, static> : never)\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function split($numberOfGroups)\n    {\n        if ($numberOfGroups < 1) {\n            throw new InvalidArgumentException('Number of groups must be at least 1.');\n        }\n\n        if ($this->isEmpty()) {\n            return $this->newInstance();\n        }\n\n        $groups = $this->newInstance();\n\n        $groupSize = floor($this->count() / $numberOfGroups);\n\n        $remain = $this->count() % $numberOfGroups;\n\n        $start = 0;\n\n        for ($i = 0; $i < $numberOfGroups; $i++) {\n            $size = $groupSize;\n\n            if ($i < $remain) {","sourceCodeStart":1367,"sourceCodeEnd":1403,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Collections/Collection.php#L1367-L1403","documentation":"Thrown by Collection::split($numberOfGroups) when $numberOfGroups < 1. Splitting into zero or negative groups is meaningless; the method requires at least one target group. An empty collection is handled separately (returns empty instance) and does not trigger this.","triggerScenarios":"Calling $collection->split(0) or ->split(-1), or passing a computed group count <= 0.","commonSituations":"Group count derived from a divisor that can be 0 (e.g. ceil(count / perGroup) when perGroup is huge); user-supplied group count not bounded.","solutions":["Clamp to 1: $collection->split(max(1, $numberOfGroups)).","Validate $numberOfGroups >= 1 before calling.","Compute group count defensively from a guaranteed-positive divisor."],"exampleFix":"// before\n$groups = $col->split($divisor); // throws if $divisor <= 0\n// after\n$groups = $col->split(max(1, (int) $divisor));","handlingStrategy":"validation","validationCode":"$groups = max(1, (int) $numberOfGroups);\n$result = $collection->split($groups);","typeGuard":"function isPositiveGroupCount(int $n): bool { return $n >= 1; }","tryCatchPattern":"try {\n    $parts = $col->split($n);\n} catch (\\InvalidArgumentException $e) {\n    $parts = $col->split(1);\n}","preventionTips":["Clamp group counts with max(1, ...).","Compute counts from guaranteed-positive divisors.","Validate user-supplied group counts before splitting."],"tags":["collections","validation","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}