{"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":926,"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":908,"sourceCodeEnd":944,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Collections/LazyCollection.php#L908-L944","documentation":"Thrown by LazyCollection::nth($step, $offset) when $step is less than 1. nth() returns every n-th element, so a step of 0 or negative is meaningless (0 would yield nothing, negative is undefined). The guard ensures a positive integer step.","triggerScenarios":"Calling $lazy->nth(0) or $lazy->nth(-2). Often $step is derived from a modulo/division that can produce 0, or from request input.","commonSituations":"Sampling every n-th row from a dataset where n is config-driven and the config is unset (0). Computing step from item count when the collection is empty.","solutions":["Pass a step >= 1; clamp with max(1, $step).","Default the source value to a sensible positive integer.","Guard the call when the step variable may be 0 or negative."],"exampleFix":"// before\n$samples = $lazy->nth($samplingRate);\n\n// after\n$samples = $lazy->nth(max(1, (int) $samplingRate));","handlingStrategy":"validation","validationCode":"$step = max(1, (int) $step);\n$result = $lazy->nth($step, $offset);","typeGuard":"function isPositiveStep($step): bool { return is_int($step) && $step >= 1; }","tryCatchPattern":"try {\n    $result = $lazy->nth($step);\n} catch (\\InvalidArgumentException $e) {\n    $result = $lazy->nth(1);\n}","preventionTips":["Default sampling-rate config values to a positive integer.","Clamp computed step values with max(1, ...)."],"tags":["laravel","collections","lazy","validation","invalid-argument"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}