{"id":"048869f7adb82551","repo":"laravel/framework","slug":"the-lazybyid-operation-was-aborted-because-the","errorCode":null,"errorMessage":"The lazyById operation was aborted because the [{$alias}] column is not present in the query result.","messagePattern":"The lazyById operation was aborted because the \\[(.+?)\\] column is not present in the query result\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Concerns/BuildsQueries.php","lineNumber":352,"sourceCode":"                $clone = clone $this;\n\n                $results = match ($descending) {\n                    SortDirection::Ascending, false => $clone->forPageAfterId($chunkSize, $lastId, $column)->get(),\n                    SortDirection::Descending, true => $clone->forPageBeforeId($chunkSize, $lastId, $column)->get(),\n                };\n\n                foreach ($results as $result) {\n                    yield $result;\n                }\n\n                if ($results->count() < $chunkSize) {\n                    return;\n                }\n\n                $lastId = $results->last()->{$alias};\n\n                if ($lastId === null) {\n                    throw new RuntimeException(\"The lazyById operation was aborted because the [{$alias}] column is not present in the query result.\");\n                }\n            }\n        });\n    }\n\n    /**\n     * Execute the query and get the first result.\n     *\n     * @param  array|string  $columns\n     * @return TValue|null\n     */\n    public function first($columns = ['*'])\n    {\n        return $this->limit(1)->get($columns)->first();\n    }\n\n    /**\n     * Execute the query and get the first result or throw an exception.","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Concerns/BuildsQueries.php#L334-L370","documentation":"Thrown inside orderedLazyById's LazyCollection generator when the alias column used as the cursor ($results->last()->{$alias}) is null on the last row of a chunk. Like the chunkById variant, this means the column is not present in the streamed rows and pagination cannot continue without looping.","triggerScenarios":"Calling ->select('name')->lazyById(500) without the id column; passing an $alias that is not selected; selecting computed columns and dropping the primary key; Eloquent with a custom primary key where the alias defaults to 'id' but the column is 'uuid'.","commonSituations":"Column pruning that omits the cursor column; custom primary key without specifying $column; DB::raw or joins producing result rows without the expected attribute; using lazyById on a query with groupBy that drops the id.","solutions":["Ensure the cursor column is selected: ->select(['id', ...other])->lazyById(500).","Specify the correct column and alias: ->lazyById(500, 'uuid', 'uuid').","Use ->select('*') to keep all columns, or add the primary key via addSelect.","Confirm the model's getKeyName() returns the right column when relying on defaults."],"exampleFix":"// before\nOrder::select('status')->lazyById(500)->each(...);\n\n// after\nOrder::select(['id','status'])->lazyById(500)->each(...);","handlingStrategy":"validation","validationCode":"$alias = $alias ?? (new $model)->getKeyName();\n$probe = $query->clone()->limit(1)->get();\nif ($probe->isNotEmpty() && ! array_key_exists($alias, (array) $probe->first())) {\n    throw new \\RuntimeException(\"Alias {$alias} missing for lazyById\");\n}","typeGuard":"function aliasPresentInSelect(\\Illuminate\\Database\\Query\\Builder $q, string $alias): bool\n{\n    $cols = $q->columns;\n    return $cols === null || in_array('*', $cols, true) || in_array($alias, $cols, true);\n}","tryCatchPattern":"try {\n    $q->lazyById(500, $column, $alias)->each($cb);\n} catch (\\RuntimeException $e) {\n    if (str_contains($e->getMessage(), 'lazyById operation was aborted')) {\n        $q->select('*')->lazyById(500, $column, $alias)->each($cb);\n    } else throw $e;\n}","preventionTips":["Include the cursor column in select() before lazyById.","Pass the model's actual primary key as $column/$alias.","Avoid groupBy/select-raw that drops the id column."],"tags":["database","lazy-collection","chunking","pagination","eloquent"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}