{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L509-L545","documentation":"When Model::preventAccessingMissingAttributes() is enabled (strict mode), accessing an attribute that is neither in the attributes array nor a relation/relation-resolver raises MissingAttributeException. This catches silent null returns caused by SELECTing a subset of columns while accessing unselected attributes.","triggerScenarios":"Enabling strict attribute access (often in tests via Model::preventAccessingMissingAttributes(true)) then doing SomeModel::select('id')->first()->title, where 'title' was not in the SELECT list. Also fires for genuinely misspelled attribute names.","commonSituations":"Strict-mode enabled globally in TestCase; using select() or addSelect() that omits a column later read by a view/serializer; column renamed in migration but code not updated; using ->only([...]) with a fresh model.","solutions":["Add the missing column to the query: ->select('id', 'title') or ->addSelect('title').","Use ->withColumns() / ensure full row hydration by removing over-restrictive select().","Confirm the attribute name spelling and that the column exists in the table/migration."],"exampleFix":"// before\nModel::preventAccessingMissingAttributes(true);\n$post = Post::select('id')->first();\necho $post->title; // throws\n\n// after\n$post = Post::select('id', 'title')->first();\necho $post->title;","handlingStrategy":"validation","validationCode":"// Before strict access, ensure columns are present\n$cols = ['id', 'title']; // required\n$row = Post::select($cols)->first();\nforeach ($cols as $c) {\n    if (! array_key_exists($c, $row->getAttributes())) {\n        throw new \\RuntimeException(\"Column {$c} not selected\");\n    }\n}","typeGuard":"function hasAttributeSelected(\\Illuminate\\Database\\Eloquent\\Model $m, string $key): bool\n{\n    return array_key_exists($key, $m->getAttributes())\n        || $m->hasGetMutator($key)\n        || $m->hasCast($key);\n}","tryCatchPattern":"try {\n    return $model->{$key};\n} catch (\\Illuminate\\Database\\Eloquent\\MissingAttributeException $e) {\n    report(\"Missing attribute {$key} on \".get_class($model));\n    return null;\n}","preventionTips":["Keep select() lists in sync with attributes touched by serializers/views.","Enable preventAccessingMissingAttributes in tests to catch regressions early.","Prefer $model->only([...]) with whitelisted fields when hydrating partial rows."],"tags":["eloquent","attribute","strict-mode","select"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}