In this article, we will see laravel 9 one to many polymorphic relationship. A one-to-many polymorphic relation is similar to a typical one-to-many relation. The child model can belong to more than one type of model using a single association. One to many polymorphic relationship is used when a model belongs to more than one other model on a single association model.
So, let's see laravel 9 polymorphic, one to many polymorphic laravel 9, polymorphic relationship laravel 9 example, laravel 9 relationships, morphMany in laravel 9, morphTo in laravel 9.
For example, users of your application can comment on posts and videos. Using polymorphic relationships, you may use a single comments table to contain comments for both posts and videos. using morphMany() and morphTo() you can access data.
In this example, we will create posts, comments, and videos tables. All tables are connected with each other like the below screenshot and we are creating migration and model all tables and retrieving data using one to many polymorphic relationships in laravel 6, laravel 7, laravel 8.
Now, we will create migration for posts, comments, and videos table. and add foreign key in the posts and videos table.
Post Table :
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Comment Table :
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->text("comments");
$table->integer('commentable_id');
$table->string("commentable_type");
$table->timestamps();
});
Video Table :
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string("title");
$table->timestamps();
});
Now, we will create Post, Comment, and Video model.
Post Model :
class Post extends Model
{
/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
Comment Model :
class Comment extends Model
{
/**
* Get the parent commentable model (post or video).
*/
public function commentable()
{
return $this->morphTo();
}
}
Video Model :
class Video extends Model
{
/**
* Get all of the video's comments.
*/
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
Once your database table and models are defined, you may access the relationships via your model's dynamic relationship properties. For example, to access all of the comments for a post, we can use the comments property.
$post = Post::find(1);
foreach ($post->comments as $comment) {
//
}
You may also retrieve the parent of a polymorphic child model by accessing the name of the method like the below code.
$comment = Comment::find(1);
$commentable = $comment->commentable;
Now, we will give you an example of creating the record for one to many polymorphic relationships like the below code.
$post = Post::find(1);
$comment = new Comment;
$comment->comments = "this is test comments from websolutionstuff";
$post->comments()->save($comment);
You might also like :
Hi there! You're in the right place if you’ve ever wanted to automate repetitive tasks in your Laravel applica...
Jan-02-2025
In this example, we will delve into the process of performing file uploads using Node.js. This tutorial will provide you...
Jul-26-2021
Welcome to this comprehensive tutorial where I'll guide you through the process of creating a new component in Angul...
Mar-22-2024
In this example, I will show you how to check if an array is empty or null in javascript or jquery. When we are wor...
Aug-18-2020