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.


Recommended Posts

View All

Laravel One to One Eloquent Relationship Tutorial


Laravel One To One Eloquent Relationships Tutorial Example, Laravel Eloquent ORM is the best possible database solution, ORM Mapping is very easy, lar...

How to inline row editing using Laravel 9


Learn how to implement inline row editing in Laravel 9 with our step-by-step guide. Make editing data faster and more efficient on your website.

Effortlessly Handle File Uploads in Laravel with FilePond


We'll walk through the process of setting up FilePond, creating a file upload form, and handling file uploads in Laravel

How to Create REST API in Laravel7 using Passport


rest api in laravel 7/6 step by step, rest api in laravel 7/6 tutorial, laravel 7/6 restful api authentication, laravel 7/6 passport rest api tutorial

How to Get Current URL in Laravel


how to get current url in laravel, laravel get current url, get current page url in laravel, get full url in laravel, find current url in laravel,