Laravel 8 One To Many Polymorphic Relationship

Websolutionstuff | Nov-19-2021 | Categories : Laravel PHP MySQL

In this tutorial we will learn about laravel 8 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 used when a model belongs to more than one other model on a single association model.

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 table. all tables are connected with each other like below screenshot and we are create migration and model all table and retrive data using one to many polymorphic relationship in laravel 6, laravel 7, laravel 8.

laravel_one_to_many_polymorphic_relationship

 

 

Create Migration :

Now, we will create migration for posts, comments and videos table. and add foreign key in 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();

});

 

 

Create Model :

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

 

 

Retrieve Record :

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 below code.

$comment = Comment::find(1);

$commentable = $comment->commentable;

 

Create Record :

Now, we will give you example of create record for one to many polymorphic relationship like 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 :

Recommended Post
Featured Post
How To Reset Modal Form In jQuery
How To Reset Modal Form In jQu...

Have you ever seen those pop-up boxes on websites? They're called modal forms, and they make it easier to do things...

Read More

Jan-04-2023

How To Remove Spaces Using JQuery
How To Remove Spaces Using JQu...

In this article, we will explain to you how to remove extra space using jquery. many times we have requirements to...

Read More

Aug-10-2020

Laravel 10 AJAX Form Validation Example
Laravel 10 AJAX Form Validatio...

In this article, we will see how to validate the ajax form in laravel 10. Here we will learn about the laravel 10 a...

Read More

Mar-24-2023

How to Force Redirect Http to Https in Laravel
How to Force Redirect Http to...

In this small artical we will see how to force redirect http to https in laravel, Here i will show you two method in&nbs...

Read More

Aug-11-2021