When one model refers to more than one other model on a single association model, the One to Many Polymorphic Model Relationship is employed. If we have posts and videos tables, for example, both need to have a comment mechanism. Then you may handle both tables in a single table. In the Laravel 6, Laravel 7, Laravel 8, and Laravel 9 apps, there is a one to many polymorphic relationship.
In this lesson, you'll learn how to establish a polymorphic one to many elegant relationship migration using a foreign key schema, use sync with a pivot table, create records, get all data, delete, update, and everything else related to one to many relationships.
I'll make a table for "posts," "videos," and "comments" in this example. Each table is linked to the others. Using the Laravel Eloquent Model, we'll now construct a one-to-many polymorphic relationship with each other. We'll start with database migration, then move on to modelling, retrieving records, and finally creating records.
"morphMany()" and "morphTo()" will be used for relation in a One to Many Polymorphic Relationship.
1. Create Migrations
Now we must migrate the tables "posts," "videos," and "comments." We'll also include a foreign key with the posts and videos tables. So, let's make something similar to what's seen here.
posts table migration
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
videos table migration
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
comments table migration
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string("body");
$table->integer('commentable_id');
$table->string("commentable_type");
$table->timestamps();
});
2. Create Models
We'll make a Post, Video, and Comment table model here. For the relationship between the two models, we'll utilise "morphMany()" and "morphTo()"
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();
}
}
?>
3. Retrieve Records
$post = Post::find(1);
dd($post->comments);
$video = Video::find(1);
dd($video->comments);
4. Create Records
$post = Post::find(1);
$comment = new Comment;
$comment->body = "Hi CodeSolutionStuff";
$post->comments()->save($comment);
$video = Video::find(1);
$comment = new Comment;
$comment->body = "Hi CodeSolutionStuff";
$video->comments()->save($comment);
I hope you will like the content and it will help you to learn Laravel One to Many Polymorphic Relationship
If you like this content, do share.