Laravel 多态关系 returns NULL
Laravel Polymorphic Relations returns NULL
我读过很多关于这个问题的帖子,但 none 对我有用。我的数据库中有一个 '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;
我读过很多关于这个问题的帖子,但 none 对我有用。我的数据库中有一个 '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;