DEV Community

Cover image for Learn Laravel Polymorphic Relationship step by step with example
Ellis
Ellis

Posted on

Learn Laravel Polymorphic Relationship step by step with example

Laravel Polymorphic Relationship allows a model to belong to more than one other model on a single association. There are three types of Polymorphic relationships in Laravel.

One To One Polymorphic Relationship

A one-to-one polymorphic relation is comparable to a simple one-to-one relation; however, the target model can belong to more than one type of model on a single association. For example, a blog Post and a User may share a polymorphic relation to the Image model.

One To Many Polymorphic Relationship

A one-to-many polymorphic relation is comparable to a simple one-to-many relation; however, the target model can belong to more than one type of model on a single association. For example, imagine users of your app can “comment” on both posts and videos.

One to One & One to Many Polymorphic Relationships

Many To Many Polymorphic Relationship

Many-to-many polymorphic relations are somewhat more complicated than morphOne and morphMany relationships. For example, a blog Post and Video model could share a polymorphic relation to the Tag model.

Many to Many Polymorphic Relationship

Oldest comments (0)