ItSolutionStuff.com

Laravel One to Many Polymorphic Relationship Tutorial

By Hardik Savani • April 16, 2024
Laravel

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, laravel 8, laravel 9, laravel 10 and laravel 11 app.

In this tutorial, you can understand how to create migration with a foreign key schema for polymorphic one to many eloquent relationship, use sync with a pivot table, create records, get all data, delete, update and everything related to one to many relationship.

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. We will first create database migration, then model, retrieve records and then how to create records too. So you can also see database table structure on below screen.

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->id();

$table->string("name");

$table->timestamps();

});

videos table migration:

Schema::create('videos', function (Blueprint $table) {

$table->id();

$table->string("name");

$table->timestamps();

});

comments table migration:

Schema::create('comments', function (Blueprint $table) {

$table->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\Models;

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\Models;

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\Models;

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 ItSolutionStuff.com";

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

$video = Video::find(1);

$comment = new Comment;

$comment->body = "Hi ItSolutionStuff.com";

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

I hope you understand of one to many polymorphic relationship...



*** Click On it and Read in Details of RelationShip types:


Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent inRandomOrder() Method Example

Read Now →

Laravel Eloquent Order By Query Example

Read Now →

Laravel Eloquent orderByRaw() Query Example

Read Now →

Laravel Relationship Eager Loading with Condition Example

Read Now →

Laravel Relationship Eager Loading with Count Example

Read Now →

Laravel Relationship Where Condition Example

Read Now →

How to use Union Query with Laravel Eloquent?

Read Now →

Laravel Eloquent Relationships Tutorial From Scratch

Read Now →

Laravel One to One Eloquent Relationship Tutorial

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →

Laravel Has Many Through Eloquent Relationship Tutorial

Read Now →

Laravel Many to Many Polymorphic Relationship Tutorial

Read Now →