{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php#L1020-L1056","documentation":"When Relation::requireMorphMap() is enabled, every morphable model must be registered in the morph map before its class name is used as a morph type. getMorphClass() throws ClassMorphViolationException if the model is absent from the map, preventing ambiguous class-name storage across deployments.","triggerScenarios":"Calling Relation::requireMorphMap(); using morphMany/morphTo/morphMap without registering the model via Relation::morphMap(['post' => Post::class]); then triggering any operation that needs the morph alias (creating a polymorphic record, serializing, whereMorphedTo with a model instance).","commonSituations":"New morphable model added without a morph-map entry; enabling requireMorphMap mid-project; package models not registered; CI/local config drift where morph map is set in AppServiceProvider but skipped in a worker.","solutions":["Register the model in the morph map, typically in AppServiceProvider::boot(): Relation::morphMap(['alias' => Model::class]).","Confirm Relation::requireMorphMap() is intended; if not, do not enable it.","Add a startup test that iterates morphable models and asserts they appear in Relation::morphMap()."],"exampleFix":"// before\nRelation::requireMorphMap();\n// Post is morphable but never registered -> throws on create\n\n// after (in AppServiceProvider::boot)\nRelation::morphMap([\n    'post'    => \\App\\Models\\Post::class,\n    'video'   => \\App\\Models\\Video::class,\n]);","handlingStrategy":"validation","validationCode":"use Illuminate\\Database\\Eloquent\\Relations\\Relation;\nif (Relation::requiresMorphMap()) {\n    foreach ([\\App\\Models\\Post::class, \\App\\Models\\Video::class] as $m) {\n        if (! in_array($m, Relation::morphMap(), true)) {\n            throw new \\RuntimeException(\"Morph map missing for {$m}\");\n        }\n    }\n}","typeGuard":"function isRegisteredInMorphMap(string $modelClass): bool\n{\n    return in_array($modelClass, \\Illuminate\\Database\\Eloquent\\Relations\\Relation::morphMap(), true);\n}","tryCatchPattern":"try {\n    return $model->getMorphClass();\n} catch (\\Illuminate\\Database\\ClassMorphViolationException $e) {\n    \\Illuminate\\Database\\Eloquent\\Relations\\Relation::morphMap([\n        strtolower(class_basename($e->model)) => $e->model,\n    ]);\n    return $model->getMorphClass();\n}","preventionTips":["Register morph maps once in AppServiceProvider::boot().","Add a startup test asserting every morphable model is mapped when requireMorphMap is on.","Avoid enabling requireMorphMap mid-project without registering all existing morph models."],"tags":["eloquent","polymorphic","morph-map","relationship","config"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}