簡體   English   中英

Laravel多態關系返回NULL

[英]Laravel Polymorphic Relations returns NULL

我已經閱讀了許多有關此問題的帖子,但沒有一個適合我。 我的數據庫中有一個“ ISA”關系。 一個可以是患者護士

class Person extends Model
{
    protected $table = 'persons';

    public function commentable()
    {
        return $this->morphTo();
    }
}

class Patient extends Model
{
    public function persons()
    {
        return $this->morphMany('App\Person', 'commentable');
    }    
}

class Nurse extends Model
{
    public function persons()
    {
        return $this->morphMany('App\Person', 'commentable');
    }
}

這是我的表以及其中的數據: 我的數據庫和其中的數據

這是我的路線:

Route::get('person', function () {
    $person = Person::find(1)->commentable();
    return json_decode(json_encode($person), true);
});

我得到一個空數組!

您必須將關系作為屬性訪問:

$person = Person::find(1)->commentable;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM