Bird
Raised Fist0
Laravelframework~20 mins

Polymorphic relationships in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of polymorphic relationships in Laravel?
easy
A. To allow a model to belong to more than one other model on a single association
B. To create many-to-many relationships between two models
C. To enforce strict foreign key constraints in the database
D. To automatically generate database migrations

Solution

  1. Step 1: Understand polymorphic relationships

    Polymorphic relationships allow a model to belong to multiple other models using a single association.
  2. 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.
  3. Final Answer:

    To allow a model to belong to more than one other model on a single association -> Option A
  4. Quick Check:

    Polymorphic relationships = single association to multiple models [OK]
Hint: Polymorphic = one model relates to many different models [OK]
Common Mistakes:
  • Confusing polymorphic with many-to-many relationships
  • Thinking it automatically creates migrations
  • Assuming it enforces database constraints
2. Which method should you use in the related model to define a polymorphic relationship?
easy
A. morphTo()
B. hasMany()
C. belongsToMany()
D. morphMany()

Solution

  1. Step 1: Identify the related model method

    The related model uses morphTo() to define the polymorphic inverse relationship.
  2. Step 2: Differentiate from owning model methods

    Owning models use morphMany(), but the related model uses morphTo().
  3. Final Answer:

    morphTo() -> Option A
  4. Quick Check:

    Related model uses morphTo() [OK]
Hint: Related model always uses morphTo() for polymorphic [OK]
Common Mistakes:
  • Using morphMany() in the related model
  • Confusing belongsToMany() with polymorphic
  • Using hasMany() instead of morphTo()
3. Given the following code, what will $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'.
medium
A. Error: Undefined property 'name'
B. 'Laravel Tips'
C. null
D. 'Comment'

Solution

  1. Step 1: Understand morphTo() behavior

    The commentable() method returns the related model instance, here a Post.
  2. Step 2: Access the related model's property

    Accessing name on the Post model returns 'Laravel Tips'.
  3. Final Answer:

    'Laravel Tips' -> Option B
  4. Quick Check:

    commentable->name = 'Laravel Tips' [OK]
Hint: morphTo() returns related model instance [OK]
Common Mistakes:
  • Expecting null if not eager loaded
  • Confusing property with method call
  • Assuming error without checking model relation
4. What is wrong with this polymorphic relationship definition?
class Image extends Model {
  public function imageable() {
    return $this->morphMany();
  }
}
medium
A. morphMany() requires parameters but none are given
B. No relationship method is needed in Image model
C. The method name should be morphable()
D. The method should be morphTo(), not morphMany()

Solution

  1. Step 1: Identify the model role

    Image is the related model, so it should use morphTo() for polymorphic inverse.
  2. Step 2: Check method used

    morphMany() is for owning models, not related models.
  3. Final Answer:

    The method should be morphTo(), not morphMany() -> Option D
  4. Quick Check:

    Related model uses morphTo() [OK]
Hint: Related model must use morphTo(), not morphMany() [OK]
Common Mistakes:
  • Using morphMany() without parameters
  • Wrong method name for polymorphic inverse
  • Thinking no method is needed
5. You want to create a polymorphic relationship where both 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?
hard
A. public function comments() { return $this->belongsToMany(Comment::class); }
B. public function comments() { return $this->hasMany(Comment::class); }
C. public function comments() { return $this->morphMany(Comment::class, 'commentable'); }
D. public function comments() { return $this->morphTo(Comment::class, 'commentable'); }

Solution

  1. Step 1: Identify owning model relationship

    Post owns many comments via polymorphic relation, so use morphMany().
  2. Step 2: Use correct parameters

    morphMany() requires the related model class and the polymorphic name 'commentable'.
  3. Final Answer:

    public function comments() { return $this->morphMany(Comment::class, 'commentable'); } -> Option C
  4. Quick Check:

    Owning model uses morphMany() with polymorphic name [OK]
Hint: Owning model uses morphMany() with polymorphic name [OK]
Common Mistakes:
  • Using hasMany() instead of morphMany()
  • Using morphTo() in owning model
  • Using belongsToMany() for polymorphic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard