🤪Laravel 9 One To Many Polymorphic Relationship Example (ok)

https://www.laravelia.com/post/laravel-9-one-to-many-polymorphic-relationship-example

Mẫu 1.1 sử dụng model App\Models\Post

C:\xampp\htdocs\songkhoe\routes\web.php

C:\xampp\htdocs\songkhoe\app\Http\Controllers\TestController.php

C:\xampp\htdocs\songkhoe\database\migrations\2022_11_21_020348_create_posts_table.php

C:\xampp\htdocs\songkhoe\database\migrations\2022_11_21_020832_create_videos_table.php

C:\xampp\htdocs\songkhoe\database\migrations\2022_11_21_020937_create_comments_table.php

C:\xampp\htdocs\songkhoe\app\Models\Post.php

C:\xampp\htdocs\songkhoe\database\factories\PostFactory.php

C:\xampp\htdocs\songkhoe\database\seeders\DatabaseSeeder.php

Mẫu 1.2 sử dụng model App\Models\Video

C:\xampp\htdocs\songkhoe\app\Models\Video.php

C:\xampp\htdocs\songkhoe\database\factories\VideoFactory.php

C:\xampp\htdocs\songkhoe\app\Models\Comment.php

In this tutorial, one to many polymorphic laravel examples, I will explain what is polymorphic relationship and when we can use it in our laravel application. One to Many Polymorphic Model Relationships is 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 a comments system.

A one-to-many polymorphic relationship in laravel is similar to a one-to-many relation. However, if the child model can belong to more than one type of model using a single association then we can use this one-to-many polymorphic relationship in laravel.

In this laravel one to many polymorphic relationship tutorial, we 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 polymorphic relationship.

laravel-9-one-to-many-polymorphic-relationship-example

One To Many (Polymorphic) Scenario

Taking the example mentioned above into consideration, we have two entities: Post and Video. To allow for comments on each of these, we can decide to set up our database like this:

PHPCopy

Model Structure of Polymorphic Relationship

Now let's define the one-to-many polymorphic relationship in our model class.

app/Models/Post.php

PHPCopy

Now define the relationship in the Video model like:

app/Models/Video.php

PHPCopy

Now define the relationship in the Comment model like:

app/Models/Comment.php

PHPCopy

Read also: Laravel 9 Many To Many | BelongsToMany Eloquent Relationship Tutorial

Retrieving The Relationship

Once our database table and models are defined, we may access the relationships via your model's dynamic relationship properties. To access the comments for a Post, we can use the image a property declared in the model.

App\Http\Controllers\TestController.php

PHPCopy

For retrieving comments for a video:

App\Http\Controllers\TestController.php

PHPCopy

Save Polymorphic Relationship Data

If we want to save or create polymorphic relationship data then we can follow the below structure:

App\Http\Controllers\TestController.php

PHPCopy

Recommended: Laravel One To One Polymorphic Relationship Example

Conclusion

We have tried to discuss the basic concept of one to many polymorphic relationships and their possible use cases in laravel applications. We should also note that one to many polymorphic relationships are not a perfect solution to everything and should only be used when convenient or feels like the right way to go.

Last updated

Was this helpful?