n How to use One To Many Polymorphic Relationship in Laravel | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

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 and laravel 8 app.

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. 

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->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 relationship of both model.

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 How are you";

$post->comments()->save($comment);

 




$video = Video::find(1);

$comment = new Comment;

$comment->body = "Lorem ipsum";

$video->comments()->save($comment);

 

You may also retrieve the owner of a polymorphic relation from the polymorphic model by accessing the name of the method that performs the call to morphTo. In our case, that is the commentable method on the Comment model. So, we will access that method as a dynamic property:

$comment = App\Models\Comment::find(1);

$commentable = $comment->commentable;

The commentable relation on the Comment model will return either a Post or Video instance, depending on which type of model owns the comment.

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook