One to Many & One to One Polymorphic Relationships - Laravel Eloquent Relationships (phần 1)
Ví dụ 1: Tạo dữ liệu cho comments, posts, users sử dụng morphMany, morphs, morphTo
Bức ảnh sau giải thích ta sử dụng morphs để xử lý nhanh 2 bảng commentable_id, commentable_type :)


C:\xampp\htdocs\hanam.com\routes\web.php
C:\xampp\htdocs\hanam.com\app\Video.php
C:\xampp\htdocs\hanam.com\app\Comment.php
C:\xampp\htdocs\hanam.com\app\Post.php
C:\xampp\htdocs\hanam.com\database\migrations\2020_08_10_143945_create_comments_table.php
C:\xampp\htdocs\hanam.com\database\migrations\2020_08_10_143922_create_videos_table.php
Ví dụ 2: Sử dụng morphTo

C:\xampp\htdocs\hanam.com\app\Comment.php
C:\xampp\htdocs\hanam.com\routes\web.php
Kết quả: tìm được dữ liệu của comment 3 là trong video

Ví dụ 3: Sử dụng morphOne
C:\xampp\htdocs\hanam.com\app\Post.php
C:\xampp\htdocs\hanam.com\routes\web.php

Previoushas-one-through & has-many-through Laravel Eloquent Relationships (phần 2)NextOne to Many & One to One Polymorphic Relationships - Laravel Eloquent Relationships (phần 2)
Last updated
Was this helpful?