0
0
Laravelframework~10 mins

Polymorphic relationships in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Polymorphic relationships
Define polymorphic relation in Model A
Define polymorphic relation in Model B
Create tables with morph columns
Save related models
Query polymorphic relation
Access related models polymorphically
This flow shows how Laravel sets up and uses polymorphic relationships by defining relations, creating tables, saving data, and querying related models.
Execution Sample
Laravel
class Comment extends Model {
  public function commentable() {
    return $this->morphTo();
  }
}

class Post extends Model {
  public function comments() {
    return $this->morphMany(Comment::class, 'commentable');
  }
}
Defines a polymorphic relation where comments can belong to multiple models like posts or videos.
Execution Table
StepActionModel StateDatabase ActionResult
1Define Comment model with morphTo relationComment model ready with commentable()No DB actionRelation method set
2Define Post model with morphMany relationPost model ready with comments()No DB actionRelation method set
3Create posts table and comments table with commentable_id and commentable_typeTables createdDB tables createdTables ready for polymorphic data
4Create Post instance and savePost instance saved with id=1Insert into postsPost record created
5Create Comment instance and associate with PostComment instance with commentable_id=1, commentable_type='App\Models\Post'Insert into commentsComment linked to Post
6Query Post commentsPost model queriedSelect comments where commentable_id=1 and commentable_type='App\Models\Post'Returns comments for Post
7Query Comment's parentComment model queriedSelect model where id=commentable_id and type=commentable_typeReturns Post model
8ExitNo further actionsNo DB actionPolymorphic relation works bidirectionally
💡 All polymorphic relations defined, data saved, and queries return expected related models.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6Final
Post instancenullPost{id=1}Post{id=1}Post{id=1}Post{id=1}
Comment instancenullnullComment{commentable_id=1, commentable_type='App\Models\Post'}Comment{...}Comment{...}
comments collectionemptyemptyempty[Comment{...}][Comment{...}]
commentable parentnullnullnullnullPost{id=1}
Key Moments - 3 Insights
Why do we need both commentable_id and commentable_type in the comments table?
Because polymorphic relations can link to different models, commentable_type stores the model class, and commentable_id stores the record id. See execution_table step 3 and 5.
How does Laravel know which model a comment belongs to when querying?
Laravel uses commentable_type to know the model class and commentable_id to find the record. This is shown in execution_table step 7.
Can a comment belong to both a Post and a Video at the same time?
No, each comment belongs to one model instance at a time, but the relation can point to different model types. This is clear from the morphTo relation in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the commentable_type value stored in the Comment instance after step 5?
A'commentable'
B'App\Models\Post'
C'Post'
D1
💡 Hint
Check the 'Model State' column in step 5 of the execution_table.
At which step does the database insert the comment linked to the post?
AStep 6
BStep 4
CStep 5
DStep 3
💡 Hint
Look for the step where commentable_id and commentable_type are set and inserted.
If we add a Video model with comments, which part of the execution flow changes?
AStep 2: Define morphMany relation in Video model
BStep 4: Create posts table
CStep 7: Query Comment's parent will fail
DStep 3: Remove commentable_type column
💡 Hint
Adding a new polymorphic model requires defining the relation method like in step 2.
Concept Snapshot
Polymorphic relationships let one model belong to multiple other models using a single association.
Use morphTo() in the child model and morphMany() or morphOne() in parent models.
Database tables need 'morph' columns: *_id and *_type to store related model info.
This allows flexible relations like comments belonging to posts or videos.
Querying uses these columns to fetch related models dynamically.
Full Transcript
Polymorphic relationships in Laravel allow a model to belong to more than one other model on a single association. The child model uses morphTo() to define the relation, while parent models use morphMany() or morphOne() to define their side. The database tables include columns like commentable_id and commentable_type to store the related model's id and class name. When saving a comment linked to a post, Laravel stores the post's id and class in these columns. Queries use this information to retrieve related models dynamically. This setup enables flexible relations such as comments belonging to posts or videos without needing separate foreign keys for each model type.