Laravel One to Many Polymorphic Relationship Tutorial (ok)

https://www.itsolutionstuff.com/post/laravel-one-to-many-polymorphic-relationship-tutorialexample.html

Một ví dụ đã hoàn thành:

Factories

C:\xampp\htdocs\test\database\factories\CommentFactory.php

<?php
namespace Database\Factories;
use App\Models\Comment;
use Illuminate\Database\Eloquent\Factories\Factory;
class CommentFactory extends Factory {
  protected $model = Comment::class;
  /**
   * Define the model's default state.
   *
   * @return array
   */
  public function definition() {
    return [
      'body'             => $this->faker->text(10),
      'commentable_id'   => $this->faker->numberBetween(1, 40),
      'commentable_type' => 'App\Models\Post',
    ];
  }
}

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

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

Migrations

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

C:\xampp\htdocs\test\database\migrations\2022_05_10_163113_create_posts_table.php

C:\xampp\htdocs\test\database\migrations\2022_05_11_042830_create_videos_table.php

C:\xampp\htdocs\test\database\migrations\2022_05_11_042938_create_comments_table.php

Seeders

C:\xampp\htdocs\test\database\seeders\CommentSeeder.php

C:\xampp\htdocs\test\database\seeders\PostSeeder.php

C:\xampp\htdocs\test\database\seeders\UserSeeder.php

C:\xampp\htdocs\test\database\seeders\VideoSeeder.php

Models

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

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

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

Controllers

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

Routes

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

Views

C:\xampp\htdocs\test\resources\views\test.blade.php

Retrieve Records:

Create Records:

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

Hoặc

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

Sql

27KB
Open

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 and laravel 9 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:

videos table migration:

comments table migration:

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:

Video Model:

Comment Model:

Retrieve Records:

Create Records:

Read Also: Laravel One to One Eloquent Relationship Tutorial

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

Last updated

Was this helpful?