Discover how one table can handle many relationships without messy code!
Why Polymorphic relationships in Laravel? - Purpose & Use Cases
Imagine you have a blog where users can comment on posts, photos, and videos. You try to create separate comment tables for each content type.
Maintaining multiple comment tables is confusing and repetitive. Adding a new content type means creating new tables and code everywhere.
Polymorphic relationships let you use one comments table for many content types, linking comments to posts, photos, or videos easily.
comments_post table, comments_photo table, comments_video table with separate queriescomments table with commentable_id and commentable_type columns, single query to fetch comments for any content
You can manage comments for many different content types with one simple, flexible database design.
A social media app where users comment on posts, photos, and events all stored in one comments table.
Polymorphic relationships reduce database complexity.
They let one table relate to multiple models easily.
Adding new related content types becomes simple and clean.
Practice
Solution
Step 1: Understand polymorphic relationships
Polymorphic relationships allow a model to belong to multiple other models using a single association.Step 2: Compare with other relationship types
Unlike many-to-many or one-to-many, polymorphic lets one model relate to different models without separate foreign keys.Final Answer:
To allow a model to belong to more than one other model on a single association -> Option AQuick Check:
Polymorphic relationships = single association to multiple models [OK]
- Confusing polymorphic with many-to-many relationships
- Thinking it automatically creates migrations
- Assuming it enforces database constraints
Solution
Step 1: Identify the related model method
The related model usesmorphTo()to define the polymorphic inverse relationship.Step 2: Differentiate from owning model methods
Owning models usemorphMany(), but the related model usesmorphTo().Final Answer:
morphTo() -> Option AQuick Check:
Related model uses morphTo() [OK]
- Using morphMany() in the related model
- Confusing belongsToMany() with polymorphic
- Using hasMany() instead of morphTo()
$comment->commentable->name output?class Comment extends Model {
public function commentable() {
return $this->morphTo();
}
}
$comment = Comment::find(1);Assuming the comment belongs to a Post with
name = 'Laravel Tips'.Solution
Step 1: Understand morphTo() behavior
Thecommentable()method returns the related model instance, here a Post.Step 2: Access the related model's property
Accessingnameon the Post model returns 'Laravel Tips'.Final Answer:
'Laravel Tips' -> Option BQuick Check:
commentable->name = 'Laravel Tips' [OK]
- Expecting null if not eager loaded
- Confusing property with method call
- Assuming error without checking model relation
class Image extends Model {
public function imageable() {
return $this->morphMany();
}
}Solution
Step 1: Identify the model role
Image is the related model, so it should usemorphTo()for polymorphic inverse.Step 2: Check method used
morphMany()is for owning models, not related models.Final Answer:
The method should be morphTo(), not morphMany() -> Option DQuick Check:
Related model uses morphTo() [OK]
- Using morphMany() without parameters
- Wrong method name for polymorphic inverse
- Thinking no method is needed
Post and Video models can have many Comment models. Which of the following is the correct way to define the relationship in the Post model?Solution
Step 1: Identify owning model relationship
Post owns many comments via polymorphic relation, so usemorphMany().Step 2: Use correct parameters
morphMany()requires the related model class and the polymorphic name 'commentable'.Final Answer:
public function comments() { return $this->morphMany(Comment::class, 'commentable'); } -> Option CQuick Check:
Owning model uses morphMany() with polymorphic name [OK]
- Using hasMany() instead of morphMany()
- Using morphTo() in owning model
- Using belongsToMany() for polymorphic
Ready to prove it?
Pass the test and this pattern moves unknown
