{"record":{"id":"502138c175110d3f","repo":"laravel/framework","slug":"the-attribute-s-either-does-not-exist-or-was-no","errorCode":null,"errorMessage":"The attribute [%s] either does not exist or was not retrieved for model [%s].","messagePattern":"The attribute \\[(.+?)\\] either does not exist or was not retrieved for model \\[(.+?)\\]\\.","errorType":"exception","errorClass":"MissingAttributeException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php","lineNumber":527,"sourceCode":"\n    /**\n     * Either throw a missing attribute exception or return null depending on Eloquent's configuration.\n     *\n     * @param  string  $key\n     * @return null\n     *\n     * @throws \\Illuminate\\Database\\Eloquent\\MissingAttributeException\n     */\n    protected function throwMissingAttributeExceptionIfApplicable($key)\n    {\n        if ($this->exists &&\n            ! $this->wasRecentlyCreated &&\n            static::preventsAccessingMissingAttributes()) {\n            if (isset(static::$missingAttributeViolationCallback)) {\n                return call_user_func(static::$missingAttributeViolationCallback, $this, $key);\n            }\n\n            throw new MissingAttributeException($this, $key);\n        }\n\n        return null;\n    }\n\n    /**\n     * Get a plain attribute (not a relationship).\n     *\n     * @param  string  $key\n     * @return mixed\n     */\n    public function getAttributeValue($key)\n    {\n        return $this->transformModelValue($key, $this->getAttributeFromArray($key));\n    }\n\n    /**\n     * Get an attribute from the $attributes array.","sourceCodeStart":509,"sourceCodeEnd":545,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L509-L545","documentation":"Thrown as MissingAttributeException when Model::preventAccessingMissingAttributes() has been enabled (strict mode) and code reads an attribute that was not selected from the database and is not a relation. The guard fires for existing, non-freshly-created models so that accidental access to columns not in the SELECT list surfaces as an error rather than silently returning null. A custom violation callback can be registered to intercept it instead of throwing.","triggerScenarios":"Calling User::select('id')->first()->email when 'email' was not selected; calling $model->some_column after a query that excluded it; accessing an attribute on a model hydrated from a partial SELECT or from raw DB values that omit the column. Only triggers when strict attribute access prevention is turned on.","commonSituations":"Enabling Model::shouldBeStrict() or preventAccessingMissingAttributes() in testing/non-production and then hitting a query that uses select() or addSelect() with a subset of columns; legacy code that accessed attributes which were later removed from a migration; aggregate/analytic queries that select computed columns only.","solutions":["Add the missing column to the SELECT list (select('id', 'email') or ->addSelect('email')).","Disable strict access selectively with Model::preventAccessingMissingAttributes(false) for the offending scope.","Register a Model::handleMissingAttributeViolationUsing(fn (...) => ...) callback to log instead of throw.","Confirm the column exists in the table schema and the model's $attributes/$casts are consistent."],"exampleFix":"// before\nModel::preventAccessingMissingAttributes(true);\n$user = User::select('id')->first();\necho $user->email; // MissingAttributeException\n\n// after\n$user = User::select('id', 'email')->first();\necho $user->email;","handlingStrategy":"validation","validationCode":"// Before reading an attribute that may be missing:\n$selected = ['id', 'name'];\nif (! in_array('email', $selected, true)) {\n    $selected[] = 'email';\n}\n$user = User::select($selected)->first();\n$explicit = $user->getAttributes();\n$ok = array_key_exists('email', $explicit);","typeGuard":"function attributeWasRetrieved(\\Illuminate\\Database\\Eloquent\\Model $model, string $key): bool\n{\n    return array_key_exists($key, $model->getAttributes());\n}","tryCatchPattern":"try {\n    return $model->{$key};\n} catch (\\Illuminate\\Database\\Eloquent\\MissingAttributeException $e) {\n    report($e);\n    return null;\n}","preventionTips":["Enable preventAccessingMissingAttributes() early in development to catch partial SELECTs.","Always select the columns you intend to read; avoid relying on null for missing columns.","Register a handleMissingAttributeViolationUsing callback to log violations without crashing in production."],"tags":["eloquent","strict-mode","attributes","query-scope"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}