{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Collections/Collection.php#L1367-L1403","documentation":"Thrown by Collection::split() when the requested number of groups is less than 1. split() divides the collection into a fixed count of roughly-equal groups, so a non-positive group count has no meaningful result. The guard exists at the top of the method before any slicing math runs. InvalidArgumentException is thrown immediately.","triggerScenarios":"Calling $collection->split(0) or $collection->split(-3). Also when a computed/groupCount variable evaluates to 0 or negative, e.g. split(count($arr) - count($arr)) or split($request->input('groups')) with an unvalidated negative input.","commonSituations":"Dynamic group counts derived from user input, request params, or database values that can be 0 when a list/section is empty. Refactoring from chunk() (which takes size) to split() (which takes count) and accidentally passing 0.","solutions":["Pass an integer >= 1 to split(); clamp the dynamic value with max(1, $count) before calling.","Validate the source variable is positive before the call (e.g. abort_if($groups < 1)).","If the count is genuinely unknown/0, guard the call site: $groups > 0 ? $c->split($groups) : $c->chunk(1)."],"exampleFix":"// before\n$parts = $collection->split($userRequestedGroups);\n\n// after\n$parts = $collection->split(max(1, (int) $userRequestedGroups));","handlingStrategy":"validation","validationCode":"$groups = max(1, (int) $numberOfGroups);\n$parts = $collection->split($groups);","typeGuard":"function isValidGroupCount($n): bool { return is_int($n) && $n >= 1; }","tryCatchPattern":"try {\n    $parts = $collection->split($n);\n} catch (\\InvalidArgumentException $e) {\n    $parts = $collection->chunk(1);\n}","preventionTips":["Never derive split() count from unvalidated user input; validate >= 1 at the controller boundary.","Centralize batch-count constants in config with positive defaults."],"tags":["laravel","collections","validation","invalid-argument"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}