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

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

Mẫu 1.1 xây dựng type model App\Models\Post

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

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

C:\xampp\htdocs\wayarmy\database\factories\UserFactory.php

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

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

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

C:\xampp\htdocs\wayarmy\app\Models\Image.php

C:\xampp\htdocs\wayarmy\database\migrations\2022_11_20_160032_create_images_table.php

C:\xampp\htdocs\wayarmy\database\migrations\2014_10_12_000000_create_users_table.php

C:\xampp\htdocs\wayarmy\database\migrations\2022_11_17_164213_create_posts_table.php

Mẫu 1.2 xây dựng type model App\Models\User

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

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

cùng xem kết quả :) 😒

https://oec.wayarmy.net/test/1

https://oec.wayarmy.net/test2/3

In this laravel 9 polymorphic relationship example, I will discuss about polymorphic relationship in laravel. In this example, I will discuss about laravel polymorphic one to one example. I will discuss about what is polymorphic relationship is and when we need this relationship in the Laravel application.

In this example, we will see polymorphic relationship laravel, laravel polymorphic one to one example, laravel polymorphic migration and laravel save polymorphic relationship. You need to just follow this tutorial to learn the above scenarios.

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

Polymorphic Relationships in Laravel

In Laravel, a polymorphic relationship allows the child model to belong to more than one type of model using a single association. For example, imagine you are building an application that allows users to share their feedback for posts and videos. In such an application, a Comment model might belong to both the Blog and Video models. Now we know what is polymorphic relationship in laravel. Now we will see when we will use it in our Laravel application.

One To One (Polymorphic) Scenario

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

PerlCopy

If we avoid polymorphic relationship schema, then our database schema will look like:

PHPCopy

But look at that our polymorphic schema, how cool it is. Here, we have three entities: Post, User, and Image. Post can have Image. User can have Image. Let's create our migration like:

PHPCopy

Read also: Laravel One To One/HasOne Eloquent Relationship Tutorial

Remember: $table→morphs('commentable') would automatically create two columns for the id and type using the text passed to it, so it will result in commentable_id and commentable_type.

Define One to One Polymorphic Relationship

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

app/Models/User.php

PHPCopy

Now define the relationship in the Post model like:

app/Models/Post.php

PHPCopy

Now define the relationship in the Image model like:

app/Models/Image.php

PHPCopy

To access the image for a User, we can use the image property declared in the model.

App\Http\Controllers\TestController.php

PHPCopy

For retrieving image on a post:

PHPCopy

Save Polymorphic Relationship Data

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

Last updated

Was this helpful?