In this example, we will create a one to many polymorphic relationship in Laravel 9. We will create a Post model, a Comment model, and a Like model. The Post model will have many Comments and many Likes. The Comment model will belong to a Post and the Like model will also belong to a Post.
First, we will create the Post model.
// app/Post.php
morphMany(‘App\Comment’, ‘commentable’);
}
public function likes()
{
return $this->morphMany(‘App\Like’, ‘likeable’);
}
}
Next, we will create the Comment model.
// app/Comment.php
morphTo();
}
}
Finally, we will create the Like model.
// app/Like.php
morphTo();
}
}
Now, we can use the Post, Comment, and Like models to create a one to many polymorphic relationship. We can create a Post, add Comments and Likes to it, and retrieve the Comments and Likes associated with the Post.
Laravel 9 one to many polymorphic relationship example; In this tutorial, you will learn about laravel one to many polymorphic relationship and how to use create, and retrieve records from database tables using this relationship.
When you work with any blog post laravel application and you have many tables like posts, videos and comments.
And you need to add comments for posts and videos. So this time you can use One to Many Polymorphic Model Relationship. Because your comments model belongs to more than one model.
Note that, using “morphMany()” and “morphTo()” method, you can create One to Many Polymorphic Relationship in your laravel eloquent models.
Laravel One to Many Polymorphic Relationship Example
Follow the following steps to create one to many polymorphic relationship and learn how to use this relationship:
Step 1: Create Migration File
First of all, create posts, videos and comments migration files with following files:
posts migration file:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
videos migration file:
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
comments migration file:
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string("body");
$table->integer('commentable_id');
$table->string("commentable_type");
$table->timestamps();
});
Step 2: Create one to many polymorphic relationships in model
Next, create one to many polymorphic relationships as follow:
Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
Video Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
Comment Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* Get all of the owning commentable models.
*/
public function commentable()
{
return $this->morphTo();
}
}
Step 3: Retrieve, create a record using polymorphic relationship
Now you will learn how to retrieve and create a record from posts, videos and comments table using one to many polymorphic relationship:
Retrieve Records:
// for posts comments
$post = Post::find(1);
dd($post->comments);
// for videos comments
$video = Video::find(1);
dd($video->comments);
Create Records:
// for posts comments
$post = Post::find(1);
$comment = new Comment;
$comment->body = "Hello world";
$post->comments()->save($comment);
// for videos comments
$video = Video::find(1);
$comment = new Comment;
$comment->body = "hello world";
$video->comments()->save($comment);
Conclusion
In this tutorial, you have learned how to create and use one to many polymorphic relationship in laravel apps.