{"record":{"id":"22d3ba1f502e4347","repo":"laravel/framework","slug":"the-chunkbyid-operation-was-aborted-because-the","errorCode":null,"errorMessage":"The chunkById operation was aborted because the [{$alias}] column is not present in the query result.","messagePattern":"The chunkById 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":214,"sourceCode":"            if ($countResults === 0) {\n                break;\n            }\n\n            if (! is_null($remaining)) {\n                $remaining = max($remaining - $countResults, 0);\n            }\n\n            // On each chunk result set, we will pass them to the callback and then let the\n            // developer take care of everything within the callback, which allows us to\n            // keep the memory low for spinning through large result sets for working.\n            if ($callback($results, $page) === false) {\n                return false;\n            }\n\n            $lastId = data_get($results->last(), $alias);\n\n            if ($lastId === null) {\n                throw new RuntimeException(\"The chunkById operation was aborted because the [{$alias}] column is not present in the query result.\");\n            }\n\n            unset($results);\n\n            $page++;\n        } while ($countResults == $count);\n\n        return true;\n    }\n\n    /**\n     * Execute a callback over each item while chunking by ID.\n     *\n     * @param  callable(TValue, int): mixed  $callback\n     * @param  int  $count\n     * @param  string|null  $column\n     * @param  string|null  $alias\n     * @return bool","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Concerns/BuildsQueries.php#L196-L232","documentation":"RuntimeException from chunkById() when, after fetching a chunk, the configured alias/id column cannot be read from the last result row (data_get returns null). chunkById paginates by remembering the last id, so a missing id column makes safe pagination impossible and the operation aborts to avoid silent skips/duplicates.","triggerScenarios":"Calling ->select('name')->chunkById(100, ...) so the id column is not selected; passing a custom $alias that does not match any selected column; using chunkById on a query with a custom alias for the primary key that the result rows do not expose; raw expressions hiding the id.","commonSituations":"Optimizing a query to select only specific columns and dropping the primary key; using chunkById on a UNION or subquery where the id column is aliased differently; passing $column but forgetting to also alias it in select().","solutions":["Ensure the id/alias column is in the SELECT list: ->select(['id', 'name'])->chunkById(...).","Pass the matching $alias argument equal to the column actually returned by the query.","Use chunk() (page-based) if you intentionally select without the id, accepting its caveats.","If using a custom primary key, pass $column = 'your_pk' and $alias = 'your_pk' consistently."],"exampleFix":"// before\nUser::select('email')->chunkById(500, function ($rows) {\n    // ...\n});\n// after\nUser::select(['id', 'email'])->chunkById(500, function ($rows) {\n    // ...\n});","handlingStrategy":"validation","validationCode":"// Ensure the chunk column is part of the SELECT before chunkById.\n$alias = 'id';\n$selected = $query->columns ?: ['*'];\nif ($selected !== ['*'] && ! in_array($alias, $selected, true)) {\n    $query->select(array_merge($selected, [$alias]));\n}\n$query->chunkById(500, $callback, $alias);","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->chunkById($size, $callback, $alias);\n} catch (\\RuntimeException $e) {\n    if (str_contains($e->getMessage(), 'column is not present in the query result')) {\n        // add the alias column to select() and restart\n    }\n    throw $e;\n}","preventionTips":["When restricting columns for chunkById/lazyById, always include the primary key or alias.","If aliasing the id (e.g. 'id as pk'), pass $alias='pk' to chunkById().","Prefer chunk()/lazy() over *ById() when you intentionally drop the id column."],"tags":["database","query-builder","chunking","pagination"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}