Polymorphic Relations (Cấu trúc Model)

https://viblo.asia/p/eloquent-relationships-in-laravel-phan-2-aRBvXWEokWE

Tiếp theo, hãy kiểm tra các định nghĩa model cần thiết để xây dựng mối quan hệ này:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    /**
     * Get all of the owning commentable models.
     */
    public function commentable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    /**
     * Get all of the post's comments.
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

class Video extends Model
{
    /**
     * Get all of the video's comments.
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

Last updated