Laravel 8 模型結(jié)構(gòu)

2021-07-19 11:36 更新

接下來(lái),看看構(gòu)建這種關(guān)聯(lián)的模型定義:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    /**
     * 獲取擁有此評(píng)論的模型
     */
    public function commentable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    /**
     * 獲取此文章的所有評(píng)論
     */
    public function comments()
    {
        return $this->morphMany('App\Models\Comment', 'commentable');
    }
}

class Video extends Model
{
    /**
     * 獲取此視頻的所有評(píng)論
     */
    public function comments()
    {
        return $this->morphMany('App\Models\Comment', 'commentable');
    }
} 
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)