Laravel Eloquent Relationships | Laravel One to Many Polymorphic Relationship Tutorial

 One to Many Polymorphic Model Relationship is 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 a comments system. Then you can manage in a single table for both tables. one to many polymorphic relationships in the laravel app.

In this tutorial, you can understand how to create migration with a foreign key schema for polymorphic one to many eloquent relationships, use sync with a pivot table, create records, get all data, delete, update, and everything related to one to many relationships.

In this example, I will create “posts”, “videos” and “comments” tables. each table is connected with each other. now we will create one to many polymorphic relationships 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 the database table structure on the below screen.

One to Many Polymorphic Relationship will use “morphMany()” and “morphTo()” for relation.

Create Migrations

Now we have to create a migration of “posts”, “videos” and “comments” tables. we will also add a foreign key with a posts, videos table. so let’s create like as below

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();
});

Create Models

Here, we will create Post, Video, and Comment table model. we will also use “morphMany()” and “morphTo()” for the relationship of both models.

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();
}
}

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 raviyatechnical";
$post->comments()->save($comment);

$video = Video::find(1);
$comment = new Comment;
$comment->body = "Hi raviyatechnical";
$video->comments()->save($comment);

No comments

Note: only a member of this blog may post a comment.