Challenge - 5 Problems
Polymorphic Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Understanding polymorphic relation retrieval
Given a Laravel polymorphic relationship where a
Comment model can belong to either a Post or a Video, what will be the output of the following code snippet?$comment = Comment::find(1); return get_class($comment->commentable);
Laravel
class Comment extends Model { public function commentable() { return $this->morphTo(); } } // Assume comment with ID 1 belongs to a Post model instance
Attempts:
2 left
💡 Hint
The
commentable relation returns the actual model instance the comment belongs to.✗ Incorrect
The
morphTo relation dynamically returns the related model instance. Since comment ID 1 belongs to a Post, get_class returns the Post class name.❓ state_output
intermediate2:00remaining
Counting polymorphic related models
In Laravel, if you have a
Assuming post ID 5 has 3 tags attached.
Tag model that can belong to many different models via a polymorphic many-to-many relationship, what will be the count of tags returned by this code?$post = Post::find(5); return $post->tags()->count();
Assuming post ID 5 has 3 tags attached.
Laravel
class Post extends Model { public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } }
Attempts:
2 left
💡 Hint
The
count() method returns the number of related models for that specific post.✗ Incorrect
The
morphToMany relation correctly fetches tags linked to the post. Since post ID 5 has 3 tags, count() returns 3.📝 Syntax
advanced2:00remaining
Correct polymorphic relation definition
Which of the following is the correct way to define a polymorphic one-to-many relationship in a Laravel model named
Photo that can belong to either a User or a Product?Attempts:
2 left
💡 Hint
The model that belongs to multiple other models uses
morphTo().✗ Incorrect
In polymorphic one-to-many, the child model uses
morphTo() to define the relation to its parent models.🔧 Debug
advanced2:00remaining
Identify the cause of error in polymorphic relation
Given this Laravel model code, why does calling
$comment->commentable cause an error?class Comment extends Model {
public function commentable() {
return $this->morphOne(Post::class, 'commentable');
}
}Attempts:
2 left
💡 Hint
Check the direction of the polymorphic relation method used.
✗ Incorrect
The child model should use
morphTo() to define the polymorphic relation, not morphOne(). Using morphOne() here causes the error.🧠 Conceptual
expert2:00remaining
Polymorphic relation database structure understanding
In Laravel polymorphic relationships, which two columns are essential in the related model's table to correctly store the polymorphic association?
Attempts:
2 left
💡 Hint
The column names combine the relation name and suffixes _id and _type.
✗ Incorrect
Laravel uses
{relation}_id and {relation}_type columns to store the ID and class name of the related model in polymorphic relations.