I have a polymorphic relationship and in my database i have one field called entity_type where the namespaces are saved ( models namespace for the polymorphic relationship )
like that normally : App\Models\MyModelName;
Is there any way to use polymorphic relationship without the full FQDN, I would like to save only MyModelName
instead of App\Models\MyModelsName
CodePudding user response:
You can map these models to what ever you would like to so you could shorten their names to what ever you like. There is a method named morphMap
and enforceMorphMap
(which calls morphMap
) on Relation
that allows you to map these classes to a shorter name:
Illuminate\Database\Eloquent\Relations\Relation::enforceMorphMap([
'post' => 'App\Models\Post',
...
]);
You can add that code to the boot
method of a Service Provider
Laravel 9.x Docs - Eloquent - Relationships - Custom Polymophic Types enforceMorphMap
CodePudding user response:
you can't save on the model names in polymorphic relationship...
Laravel stores the model name with its namespace in other to avoid conflicts with Models which might have thesame name