0
0
Laravelframework~20 mins

Polymorphic relationships in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Polymorphic Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
AApp\Models\Post
BApp\Models\Video
CIlluminate\Database\Eloquent\Relations\MorphTo
DApp\Models\Comment
Attempts:
2 left
💡 Hint
The commentable relation returns the actual model instance the comment belongs to.
state_output
intermediate
2:00remaining
Counting polymorphic related models
In Laravel, if you have a 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');
  }
}
AThrows an error because morphToMany is not defined
B0
C3
DCount of all tags in the database
Attempts:
2 left
💡 Hint
The count() method returns the number of related models for that specific post.
📝 Syntax
advanced
2: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?
A
public function imageable() {
  return $this->belongsTo();
}
B
public function imageable() {
  return $this->morphTo();
}
C
public function imageable() {
  return $this->morphMany();
}
D
public function imageable() {
  return $this->hasMany();
}
Attempts:
2 left
💡 Hint
The model that belongs to multiple other models uses morphTo().
🔧 Debug
advanced
2: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');
  }
}
AThe Comment model must use morphMany instead
BThe relation name 'commentable' is invalid
CThe Post class does not exist
DUsing morphOne instead of morphTo causes the error
Attempts:
2 left
💡 Hint
Check the direction of the polymorphic relation method used.
🧠 Conceptual
expert
2: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?
Acommentable_id and commentable_type
Bid and type
Crelated_id and related_type
Dmorph_id and morph_type
Attempts:
2 left
💡 Hint
The column names combine the relation name and suffixes _id and _type.