Skip to content

Custom Polymorphic Types

By default, Fedaco will use the fully qualified class name to store the "type" of the related model. For instance, given the one-to-many relationship example above where a Comment model may belong to a Post or a Video model, the default commentable_type would be either Post or Video, respectively. However, you may wish to decouple these values from your application's internal structure.

For example, instead of using the model names as the "type", we may use simple strings such as post and video. By doing so, the polymorphic "type" column values in our database will remain valid even if the models are renamed:

typescript
Relation.morphMap({
  'post': 'Post',
  'video': 'Video'
});

Released under the MIT License.