{"record":{"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/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Concerns/BuildsQueries.php#L334-L370","documentation":"RuntimeException from orderedLazyById() (lazyById) when the alias/id column of the last row in a chunk is null - i.e. the column is not present on the result objects. Because lazyById streams by tracking the last id, a missing id makes continued paging unsafe and the operation aborts.","triggerScenarios":"Calling ->select('name')->lazyById(...) excluding the id column; passing a custom $alias not matching the selected column; selecting into arrays/DTOs where the id attribute name differs from the alias.","commonSituations":"Column pruning for performance that drops the primary key; aliasing the id in the query (e.g. ->select('id as identifier')) without passing $alias='identifier'; using lazyById with a model that maps columns to differently-named attributes.","solutions":["Include the id/alias column in the select: ->select(['id', 'name'])->lazyById(...).","Pass $alias matching the actual returned attribute/column name.","If you must select without the id, use lazy() instead and accept page-based behavior.","For custom primary keys, set protected $primaryKey on the model and/or pass $column explicitly."],"exampleFix":"// before\nUser::select('email')->lazyById(500)->each(fn ($u) => ...);\n// after\nUser::select(['id', 'email'])->lazyById(500)->each(fn ($u) => ...);","handlingStrategy":"validation","validationCode":"// Make sure the alias/id column is in the result before lazyById.\n$alias = 'id';\n$cols = $query->getQuery()->columns;\nif ($cols !== null && ! in_array('*', $cols, true) && ! in_array($alias, $cols, true)) {\n    $query->select(array_merge($cols, [$alias]));\n}\n$query->lazyById(500, column: $alias, alias: $alias)->each($callback);","typeGuard":"function chunkColumnIsSelected(\\Illuminate\\Database\\Eloquent\\Builder $q, string $alias): bool\n{\n    $cols = $q->getQuery()->columns;\n    return $cols === null || in_array('*', $cols, true) || in_array($alias, $cols, true);\n}","tryCatchPattern":"try {\n    $query->lazyById($size)->each($callback);\n} catch (\\RuntimeException $e) {\n    if (str_contains($e->getMessage(), 'column is not present in the query result')) {\n        // add the alias/id column to select() and rerun\n    }\n    throw $e;\n}","preventionTips":["Never prune the primary key out of the select when using *ById streaming.","Pass the matching $alias when the id column is selected under a different name.","Use lazy() (page-based) when the id column legitimately must be absent."],"tags":["database","query-builder","lazy-collection","pagination"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}