Laravel One to Many Polymorphic Relationship Tutorial
One to Many Polymorphic Model Relationship used when a model belongs to more than one other model on a single association model. For example, If we have posts and videos tables, both need to add comments system. Then you can manage in a single table for both tables. one to many polymorphic relationship in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 app.
In this tutorial, you can understand how to create migration with a foreign key schema for polymorphic one to many eloquent relationship, use sync with a pivot table, create records, get all data, delete, update and everything related to one to many relationship.
In this example, i will create "posts", "videos" and "comments" table. each table is connected with each other. now we will create one to many polymorphic relationship with each other by using the laravel Eloquent Model. We will first create database migration, then model, retrieve records and then how to create records too. So you can also see database table structure on below screen.
One to Many Polymorphic Relationship will use "morphMany()" and "morphTo()" for relation.
Create Migrations:
Now we have to create migration of "posts", "videos" and "comments" table. we will also add foreign key with posts, videos table. so let's create like as below:
posts table migration:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->timestamps();
});
videos table migration:
Schema::create('videos', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->timestamps();
});
comments table migration:
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->string("body");
$table->integer('commentable_id');
$table->string("commentable_type");
$table->timestamps();
});
Create Models:
Here, we will create Post, Video and Comment table model. we will also use "morphMany()" and "morphTo()" for relationship of both model.
Post Model:
<?php
namespace App\Models;
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\Models;
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\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* Get all of the owning commentable models.
*/
public function commentable()
{
return $this->morphTo();
}
}
Retrieve Records:
$post = Post::find(1);
dd($post->comments);
$video = Video::find(1);
dd($video->comments);
Create Records:
$post = Post::find(1);
$comment = new Comment;
$comment->body = "Hi ItSolutionStuff.com";
$post->comments()->save($comment);
$video = Video::find(1);
$comment = new Comment;
$comment->body = "Hi ItSolutionStuff.com";
$video->comments()->save($comment);
I hope you understand of one to many polymorphic relationship...
*** Click On it and Read in Details of RelationShip types:
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent inRandomOrder() Method Example
- Laravel Eloquent Order By Query Example
- Laravel Eloquent orderByRaw() Query Example
- Laravel Relationship Eager Loading with Condition Example
- Laravel Relationship Eager Loading with Count Example
- Laravel Relationship Where Condition Example
- How to use Union Query with Laravel Eloquent?
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel One to One Eloquent Relationship Tutorial
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel Many to Many Polymorphic Relationship Tutorial