{"record":{"id":"0a44a1dec9401c7a","repo":"laravel/framework","slug":"no-morph-map-defined-for-model-class","errorCode":null,"errorMessage":"No morph map defined for model [{$class}].","messagePattern":"No morph map defined for model \\[(.+?)\\]\\.","errorType":"exception","errorClass":"ClassMorphViolationException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php","lineNumber":1038,"sourceCode":"     *\n     * @return string\n     *\n     * @throws \\Illuminate\\Database\\ClassMorphViolationException\n     */\n    public function getMorphClass()\n    {\n        $morphMap = Relation::morphMap();\n\n        if (! empty($morphMap) && in_array(static::class, $morphMap)) {\n            return array_search(static::class, $morphMap, true);\n        }\n\n        if (static::class === Pivot::class) {\n            return static::class;\n        }\n\n        if (Relation::requiresMorphMap()) {\n            throw new ClassMorphViolationException($this);\n        }\n\n        return static::class;\n    }\n\n    /**\n     * Create a new model instance for a related model.\n     *\n     * @template TRelatedModel of \\Illuminate\\Database\\Eloquent\\Model\n     *\n     * @param  class-string<TRelatedModel>  $class\n     * @return TRelatedModel\n     */\n    protected function newRelatedInstance($class)\n    {\n        return tap(new $class, function ($instance) {\n            if (! $instance->getConnectionName()) {\n                $instance->setConnection($this->getConnectionName());","sourceCodeStart":1020,"sourceCodeEnd":1056,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php#L1020-L1056","documentation":"Thrown as ClassMorphViolationException (message: 'No morph map defined for model [{$class}].') by getMorphClass() when Relation::requiresMorphMap() is true and the current model class is not registered in the morph map. Morph maps alias model classes to short keys stored in polymorphic morph_type columns; if morph maps are required and the model has no alias, Laravel refuses to write a raw FQCN into the morph map and throws instead.","triggerScenarios":"Enforcing morph maps via Relation::requireMorphMap() and then creating/reading a polymorphic relation involving a model that was never registered with Relation::morphMap([...]). getMorphClass() is invoked when serializing morph types (saving a morphTo / morphMany, building morph type constraints, queueing, etc.).","commonSituations":"Adding a new model to a polymorphic system after enabling requireMorphMap() without extending the morphMap() call; refactoring model namespaces without updating the morph map aliases; third-party packages that define their own models but are not added to the app's morph map.","solutions":["Register the model in the morph map: Relation::morphMap(['alias' => App\\Models\\Tag::class]) (typically in AppServiceProvider boot()).","Disable the requirement if raw class names are acceptable: Relation::requireMorphMap(false).","Ensure every model participating in morphTo/morphMany relations has an alias when requireMorphMap() is on.","If renaming a model, update both the morph map alias target and migrate existing morph_type column values."],"exampleFix":"// before\nRelation::requireMorphMap();\n// Tag not registered -> ClassMorphViolationException when used in morphTo\n\n// after\n// in AppServiceProvider::boot()\nRelation::morphMap([\n    'tag' => App\\Models\\Tag::class,\n]);","handlingStrategy":"validation","validationCode":"use Illuminate\\Database\\Eloquent\\Relations\\Relation;\n\n$models = [App\\Models\\Tag::class, App\\Models\\Post::class];\n$registered = Relation::morphMap();\nforeach ($models as $class) {\n    if (Relation::requiresMorphMap() && ! in_array($class, $registered, true)) {\n        throw new \\LogicException(\"{$class} has no morph map alias; register it via Relation::morphMap().\");\n    }\n}","typeGuard":"function modelHasMorphMap(string $class): bool\n{\n    return in_array($class, \\Illuminate\\Database\\Eloquent\\Relations\\Relation::morphMap(), true);\n}","tryCatchPattern":null,"preventionTips":["Register every polymorphic model in Relation::morphMap() before enabling requireMorphMap().","When renaming a model, update the morph map target and migrate morph_type values in the same change.","Add a boot test asserting all polymorphic models are present in the morph map."],"tags":["eloquent","polymorphic","morph-map","relations","configuration"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}