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 9 GEO Chart Example using LavaCharts


Learn how to create interactive GEO charts with Laravel 9 and LavaCharts. Visualize your data on maps and improve data analysis. Check it out now!

How To Use Chart JS In Laravel 


laravel chart js, laravel chartjs ajax example, how to create chart in laravel, how to use chart js in laravel, chartjs laravel example, laravel chart...

Laravel 9 Captcha Tutorial – Create Captcha in Laravel


A CAPTCHA is a challenge-response test used in computing to determine if a user is human. It is an abbreviation for the Completely Automated Public Tu...

Laravel 9 Socialite Login with LinkedIn Tutorial Example


How to use the Laravel socialite, Livewire, and Jetstream libraries to create a LinkedIn login system from scratch in Laravel

Laravel where and orWhere Condition Example


how to use where and orwhere condition in laravel 8, laravel 8 where and orwhere condition, where and orwhere laravel 8 example, orwhere and where in...