{"id":"9c130638c61bb93d","repo":"laravel/framework","slug":"generators-should-not-be-passed-directly-to-lazyco","errorCode":null,"errorMessage":"Generators should not be passed directly to LazyCollection. Instead, pass a generator function.","messagePattern":"Generators should not be passed directly to LazyCollection\\. Instead, pass a generator function\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/LazyCollection.php","lineNumber":54,"sourceCode":"     * @var (Closure(): \\Generator<TKey, TValue, mixed, void>)|static|array<TKey, TValue>\n     */\n    public $source;\n\n    /**\n     * Create a new lazy collection instance.\n     *\n     * @param  \\Illuminate\\Contracts\\Support\\Arrayable<TKey, TValue>|iterable<TKey, TValue>|(Closure(): \\Generator<TKey, TValue, mixed, void>)|self<TKey, TValue>|array<TKey, TValue>|null  $source\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function __construct($source = null)\n    {\n        if ($source instanceof Closure || $source instanceof self) {\n            $this->source = $source;\n        } elseif (is_null($source)) {\n            $this->source = static::empty();\n        } elseif ($source instanceof Generator) {\n            throw new InvalidArgumentException(\n                'Generators should not be passed directly to LazyCollection. Instead, pass a generator function.'\n            );\n        } else {\n            $this->source = $this->getArrayableItems($source);\n        }\n    }\n\n    /**\n     * Create a new instance of the collection.\n     *\n     * @param  \\Illuminate\\Contracts\\Support\\Arrayable<TKey, TValue>|iterable<TKey, TValue>|(Closure(): \\Generator<TKey, TValue, mixed, void>)|self<TKey, TValue>|array<TKey, TValue>|null  $items\n     * @return static\n     */\n    protected function newInstance($items = [])\n    {\n        return new static($items);\n    }\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Collections/LazyCollection.php#L36-L72","documentation":"Thrown by the LazyCollection constructor when a Generator instance is passed directly instead of a Closure that produces a generator. LazyCollection is lazy precisely because it wraps a factory (Closure) it can invoke multiple times; a bare Generator is single-use and would be exhausted on first iteration, breaking re-iteration. Passing it as a function preserves reusability.","triggerScenarios":"new LazyCollection(my_generator_function()) where the parens invoke the generator. Or LazyCollection::make($gen) with $gen already a Generator. Anywhere a yield-based function is called rather than referenced.","commonSituations":"Writing new LazyCollection(function () { yield ... }) but accidentally calling another generator function inline: new LazyCollection($this->rows()). Also wrapping an existing iterator/generator from a third-party library directly.","solutions":["Pass a Closure that returns the generator: new LazyCollection(fn () => $this->rows()).","Pass the function reference without invoking: new LazyCollection([$this, 'rows']).","If re-iteration isn't needed, materialize with collect() or pass an array instead."],"exampleFix":"// before\n$lazy = new LazyCollection($this->generateRows());\n\n// after\n$lazy = new LazyCollection(fn () => $this->generateRows());","handlingStrategy":"type-guard","validationCode":"if ($source instanceof \\Generator) {\n    $original = $source;\n    $source = fn () => $original; // wrap into a factory\n}\n$lazy = new \\Illuminate\\Support\\LazyCollection($source);","typeGuard":"function isGeneratorFactory($src): bool {\n    return $src instanceof \\Closure || $src instanceof \\Illuminate\\Support\\LazyCollection || ! ($src instanceof \\Generator);\n}","tryCatchPattern":"try {\n    $lazy = new \\Illuminate\\Support\\LazyCollection($src);\n} catch (\\InvalidArgumentException $e) {\n    $lazy = new \\Illuminate\\Support\\LazyCollection(fn () => $src);\n}","preventionTips":["Always pass a Closure (fn () => generator) to LazyCollection, never an invoked generator.","Static-analyze with PHPStan/Psalm at the LazyCollection constructor to catch Generator args."],"tags":["laravel","collections","lazy","generators","invalid-argument"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}