{"record":{"id":"e4117061240b1cfa","repo":"laravel/framework","slug":"the-chunk-size-should-be-at-least-1","errorCode":null,"errorMessage":"The chunk size should be at least 1","messagePattern":"The chunk size should be at least 1","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Concerns/BuildsQueries.php","lineNumber":256,"sourceCode":"                if ($callback($value, (($page - 1) * $count) + $key) === false) {\n                    return false;\n                }\n            }\n        }, $column, $alias);\n    }\n\n    /**\n     * Query lazily, by chunks of the given size.\n     *\n     * @param  int  $chunkSize\n     * @return \\Illuminate\\Support\\LazyCollection<int, TValue>\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function lazy($chunkSize = 1000)\n    {\n        if ($chunkSize < 1) {\n            throw new InvalidArgumentException('The chunk size should be at least 1');\n        }\n\n        $this->enforceOrderBy();\n\n        return new LazyCollection(function () use ($chunkSize) {\n            $page = 1;\n\n            while (true) {\n                $results = $this->forPage($page++, $chunkSize)->get();\n\n                foreach ($results as $result) {\n                    yield $result;\n                }\n\n                if ($results->count() < $chunkSize) {\n                    return;\n                }\n            }","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Concerns/BuildsQueries.php#L238-L274","documentation":"InvalidArgumentException from lazy() when $chunkSize is less than 1. lazy() streams results in pages, so a non-positive chunk size is invalid; the guard rejects it up front before any query runs.","triggerScenarios":"Calling ->lazy(0), ->lazy(-1), or passing a config/computed value that evaluates to < 1 (e.g. null coalesced wrong, env var parsed as 0).","commonSituations":"Reading chunk size from config/env where the variable is unset and defaults to 0; dynamic sizing math that can produce 0 for small datasets; copy-paste from chunk() with a placeholder value.","solutions":["Pass a positive integer: ->lazy(500).","Clamp the value before calling: ->lazy(max(1, (int) config('app.chunk_size', 1000))).","Validate the config value at boot and fail loudly with a clearer message if it is non-positive."],"exampleFix":"// before\n$rows = Model::lazy((int) env('CHUNK_SIZE')); // 0 when unset\n// after\n$rows = Model::lazy(max(1, (int) env('CHUNK_SIZE', 1000)));","handlingStrategy":"validation","validationCode":"$chunkSize = max(1, (int) env('CHUNK_SIZE', 1000));\nif ($chunkSize < 1) {\n    throw new InvalidArgumentException('lazy() chunk size must be >= 1');\n}\nModel::lazy($chunkSize)->each($callback);","typeGuard":"function isValidChunkSize(int $size): bool\n{\n    return $size >= 1;\n}","tryCatchPattern":"try {\n    Model::lazy($size)->each($callback);\n} catch (\\InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'chunk size should be at least 1')) {\n        Model::lazy(max(1, $size))->each($callback);\n    }\n    throw $e;\n}","preventionTips":["Clamp env-derived chunk sizes with max(1, ...) at the call site.","Validate numeric config keys during application boot.","Default chunk sizes to a sane positive number (e.g. 1000)."],"tags":["database","query-builder","lazy-collection","validation"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}