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 whereHas and orWhereHas Query Example


laravel whereHas and orWhereHas query example, wherehas and orWhereHas query in laravel, how to use wherehas in laravel

Laravel 9 PHP Guzzle Http Client Examples


We will provide answers in this manual. If you've been searching the internet for a Laravel Guzzle http client example, your search is over.

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 Eloquent Query - Laravel whereKey method


We'll look at the whereKey method in Laravel Eloquent in this post. whereKey is an extremely easy-to-use tool with a lot of potential.

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,