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 : AppModelsMyModelName;
Is there any way to use polymorphic relationship without the full FQDN, I would like to save only MyModelName
instead of AppModelsMyModelsName
Advertisement
Answer
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:
IlluminateDatabaseEloquentRelationsRelation::enforceMorphMap([ 'post' => 'AppModelsPost', ... ]);
You can add that code to the boot
method of a Service Provider
Laravel 9.x Docs – Eloquent – Relationships – Custom Polymophic Types enforceMorphMap