[英]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.