{"record":{"id":"caea6bbe26df1dfb","repo":"mongodb/laravel-mongodb","slug":"method-s-initializemodelattributes-requires-laravel-13-or","errorCode":null,"errorMessage":"Method %s::initializeModelAttributes() requires Laravel 13 or later. It is called by the model constructor and must not be called directly.","messagePattern":"Method (.+?)::initializeModelAttributes\\(\\) requires Laravel 13 or later\\. It is called by the model constructor and must not be called directly\\.","errorType":"exception","errorClass":"BadMethodCallException","httpStatus":null,"severity":"error","filePath":"src/Eloquent/DocumentModel.php","lineNumber":105,"sourceCode":"        $value ??= $this->attributes['id'] ?? $this->attributes['_id'] ?? null;\n\n        // Convert ObjectID to string.\n        if ($value instanceof ObjectID) {\n            return (string) $value;\n        }\n\n        if ($value instanceof Binary) {\n            return (string) $value->getData();\n        }\n\n        return $value;\n    }\n\n    /** @inheritdoc */\n    public function initializeModelAttributes()\n    {\n        if (! method_exists(parent::class, 'initializeModelAttributes')) {\n            throw new BadMethodCallException(sprintf('Method %s::initializeModelAttributes() requires Laravel 13 or later. It is called by the model constructor and must not be called directly.', static::class));\n        }\n\n        parent::initializeModelAttributes();\n\n        $keyType = static::resolveClassAttribute(Table::class)?->keyType;\n\n        if ($keyType === null) {\n            return;\n        }\n\n        $declaringClass = (new ReflectionProperty($this, 'keyType'))->getDeclaringClass()->getName();\n\n        if ($declaringClass !== Model::class && $declaringClass !== MongoDBModel::class) {\n            return;\n        }\n\n        $this->keyType = $keyType;\n    }","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/mongodb/laravel-mongodb/blob/0634653039468ceb0268a69192bdac64469ed043/src/Eloquent/DocumentModel.php#L87-L123","documentation":"DocumentModel::initializeModelAttributes() hooks into Laravel 13's model-constructor attribute-initialization API. If the parent Eloquent Model lacks that method (Laravel 12 or earlier), the trait throws BadMethodCallException. It is intended to be invoked only by the model constructor internals.","triggerScenarios":"Calling initializeModelAttributes() directly on a Document-derived model; running the package with Laravel 12 or older while using a model based on DocumentModel; manually invoking the method from custom constructor overrides.","commonSituations":"Upgrading laravel-mongodb ahead of (or behind) the framework version; copying code that touches the method from an older integration guide; overriding the model constructor and calling the method explicitly.","solutions":["Upgrade Laravel to 13 or later when using DocumentModel-based models","Do not call initializeModelAttributes() manually — let the model constructor do it","If on Laravel <=12, use the older BaseModel integration path of laravel-mongodb instead of DocumentModel","Remove explicit calls from custom constructor overrides"],"exampleFix":"// before\npublic function __construct(array $attributes = [])\n{\n    parent::__construct($attributes);\n    $this->initializeModelAttributes();\n}\n// after\npublic function __construct(array $attributes = [])\n{\n    parent::__construct($attributes); // constructor initializes attributes on Laravel 13+\n}","handlingStrategy":"validation","validationCode":"if (!method_exists(Model::class, 'initializeModelAttributes')) {\n    throw new RuntimeException('DocumentModel requires Laravel 13+');\n}","typeGuard":"function supportsDocumentModel(): bool {\n    return method_exists(\\Illuminate\\Database\\Eloquent\\Model::class, 'initializeModelAttributes');\n}","tryCatchPattern":"try {\n    $model = new MyDocumentModel();\n} catch (BadMethodCallException $e) {\n    if (str_contains($e->getMessage(), 'requires Laravel 13')) {\n        // upgrade Laravel or switch to the legacy base model\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Check laravel/framework version compatibility before upgrading laravel-mongodb","Never call initializeModelAttributes() manually; rely on the constructor","Pin framework versions in composer.json so trait and framework stay in sync"],"tags":["eloquent","compatibility","version"],"backgroundTag":"incompatible-source-type","analyzedSha":"0634653039468ceb0268a69192bdac64469ed043","analyzedAt":"2026-09-15T02:56:37.067Z","contentChangedAt":"2026-09-15T02:56:37.067Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}