{"record":{"id":"2dbb85c398b8ddb3","repo":"laravel/framework","slug":"step-value-must-be-at-least-1-2dbb85","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/LazyCollection.php","lineNumber":928,"sourceCode":"    #[\\Override]\n    public function union($items)\n    {\n        return $this->passthru(__FUNCTION__, func_get_args());\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        return new static(function () use ($step, $offset) {\n            $position = 0;\n\n            foreach ($this->slice($offset) as $item) {\n                if ($position % $step === 0) {\n                    yield $item;\n                }\n\n                $position++;\n            }\n        });\n    }\n\n    /**\n     * Get the items with the specified keys.\n     *","sourceCodeStart":910,"sourceCodeEnd":946,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Collections/LazyCollection.php#L910-L946","documentation":"Thrown by LazyCollection::nth($step, $offset) when $step < 1. nth yields every $step-th element using modulo ($position % $step === 0); a step of zero would cause division-by-zero and a negative step makes no sense, so the method rejects anything below 1 with an InvalidArgumentException. The PHPDoc marks step as positive-int.","triggerScenarios":"->nth(0), ->nth(-2); step derived from a divisor that can be zero; user-controlled 'sample every Nth' input.","commonSituations":"Downsampling data with a configurable interval that defaults to 0; passing a computed step from ceil(count/N) where N exceeds count.","solutions":["Ensure step >= 1: ->nth(max(1, $step)).","Validate the input before calling nth and short-circuit for invalid values.","Default the parameter explicitly to a known-good value when sourced from config."],"exampleFix":"// before\nreturn $collection->nth($request->integer('interval'));\n// after\n$interval = max(1, $request->integer('interval', 1));\nreturn $collection->nth($interval);","handlingStrategy":"validation","validationCode":"if ($step < 1) {\n    throw new \\InvalidArgumentException('nth step must be >= 1');\n}\nreturn $collection->nth($step);","typeGuard":"$step = (is_int($step) && $step >= 1) ? $step : 1;","tryCatchPattern":"try {\n    return $collection->nth($step);\n} catch (\\InvalidArgumentException $e) {\n    return $collection->nth(1);\n}","preventionTips":["Clamp sample intervals with max(1, $value).","Add request validation 'interval' => 'integer|min:1'.","Default configurable intervals to 1, not 0."],"tags":["lazy-collection","validation","argument-error","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}