{"record":{"id":"7582ad66d4370100","repo":"laravel/framework","slug":"step-value-must-be-at-least-1","errorCode":null,"errorMessage":"Step value must be at least 1.","messagePattern":"Step value must be at least 1\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/Collection.php","lineNumber":961,"sourceCode":"     */\n    public function union($items)\n    {\n        return $this->newInstance($this->items + $this->getArrayableItems($items));\n    }\n\n    /**\n     * Create a new collection consisting of every n-th element.\n     *\n     * @param  int  $step\n     * @param  int  $offset\n     * @return ($step is positive-int ? static : never)\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function nth($step, $offset = 0)\n    {\n        if ($step < 1) {\n            throw new InvalidArgumentException('Step value must be at least 1.');\n        }\n\n        $new = [];\n\n        $position = 0;\n\n        foreach ($this->slice($offset)->items as $item) {\n            if ($position % $step === 0) {\n                $new[] = $item;\n            }\n\n            $position++;\n        }\n\n        return $this->newInstance($new);\n    }\n\n    /**","sourceCodeStart":943,"sourceCodeEnd":979,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Collections/Collection.php#L943-L979","documentation":"Thrown by Collection::nth() when $step is less than 1. nth() selects every n-th element; a step of 0 or negative is mathematically meaningless (modulo by zero / infinite loop) and is rejected up front.","triggerScenarios":"Calling $collection->nth(0) or ->nth(-1), or passing a computed step that evaluates to <=0.","commonSituations":"Dynamic step derived from user input or division that rounds to 0; off-by-one in a loop counter.","solutions":["Ensure $step >= 1: max(1, (int) $step).","Validate the input before calling nth().","Default to a positive literal when the parameter is optional."],"exampleFix":"// before\n$every = $col->nth($userStep); // throws if $userStep = 0\n// after\n$every = $col->nth(max(1, (int) $userStep));","handlingStrategy":"validation","validationCode":"$step = max(1, (int) $step);\n$result = $collection->nth($step, $offset);","typeGuard":"function isPositiveStep(int $step): bool { return $step >= 1; }","tryCatchPattern":"try {\n    $every = $col->nth($userStep);\n} catch (\\InvalidArgumentException $e) {\n    $every = $col->nth(1);\n}","preventionTips":["Clamp dynamic step values with max(1, ...).","Validate numeric inputs from users before passing.","Use a positive literal default for optional step parameters."],"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"}