Polymorphic relationships let you connect one model to many different models using a single association. This saves time and keeps your database simple.
0
0
Polymorphic relationships in Laravel
Introduction
You want to add comments to posts, videos, and photos without making separate comment tables.
You want to tag different types of content like articles and products with the same tags.
You want to track likes or favorites on multiple models like posts and comments.
You want to attach images to different models like users, products, and categories.
Syntax
Laravel
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
// In Comment model
public function commentable()
{
return $this->morphTo();
}The morphMany method defines the polymorphic one-to-many side.
The morphTo method defines the inverse side that can belong to any model.
Examples
This lets a post have many comments.
Laravel
class Post extends Model { public function comments() { return $this->morphMany(Comment::class, 'commentable'); } }
This lets a video have many comments using the same comments table.
Laravel
class Video extends Model { public function comments() { return $this->morphMany(Comment::class, 'commentable'); } }
This lets a comment belong to either a post or a video.
Laravel
class Comment extends Model { public function commentable() { return $this->morphTo(); } }
Sample Program
This example shows how comments can belong to either posts or videos using polymorphic relationships. The commentable_type and commentable_id fields tell Laravel which model the comment belongs to.
Laravel
<?php use Illuminate\Database\Eloquent\Model; class Post extends Model { public function comments() { return $this->morphMany(Comment::class, 'commentable'); } } class Video extends Model { public function comments() { return $this->morphMany(Comment::class, 'commentable'); } } class Comment extends Model { public function commentable() { return $this->morphTo(); } } // Usage example $post = new Post(); $post->id = 1; $video = new Video(); $video->id = 2; $comment1 = new Comment(); $comment1->id = 101; $comment1->commentable_type = Post::class; $comment1->commentable_id = $post->id; $comment2 = new Comment(); $comment2->id = 102; $comment2->commentable_type = Video::class; $comment2->commentable_id = $video->id; // Simulate fetching commentable model function getCommentableModel(Comment $comment) { match ($comment->commentable_type) { Post::class => print("Comment {$comment->id} belongs to Post {$comment->commentable_id}\n"), Video::class => print("Comment {$comment->id} belongs to Video {$comment->commentable_id}\n"), default => print("Unknown commentable type\n"), }; } getCommentableModel($comment1); getCommentableModel($comment2);
OutputSuccess
Important Notes
Remember to add commentable_type and commentable_id columns in your comments table.
Polymorphic relationships reduce the need for multiple tables for similar relations.
Summary
Polymorphic relationships let one model relate to many different models.
Use morphMany on the owning models and morphTo on the related model.
This keeps your database simple and your code clean.