问题描述
我正确存储了数据,但是当我尝试在编辑功能中获取数据时,它显示了此信息 错误
SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ staff.staff_id”(SQL:选择users
。*,staff
。staff_id
作为{ {1}},pivot_staff_id
。staff
,user_id
,pivot_user_id
。staff
,staff_type
,pivot_staff_type
。 }}分别为staff
的{{1}},role
。pivot_role
和staff
的{{1}}。created_at
pivot_created_at
上的内部联接staff
。updated_at
= pivot_updated_at
。users
其中staff
。users
在(2)中,{{ 1}}。id
= App \ Models \ Investor)
staff
解决方法
您不关注naming conventions,因此您应确定该关系的 foreign key
和 "other key"
:
public function staff()
{
return $this->morphToMany(User::class,'staff','name of table','foreignPivotKey','relatedPivotKey);
}
我不知道您的表列名称,但在您的情况下,可能如下所示:
public function staff()
{
return $this->morphToMany(User::class,'staffabels','staffabel_id','staff_id')
->withPivot(['role'])
->withTimestamps();
}
public function investors()
{
return $this->morphedByMany(Investor::class,'staff_id','staffable_id');
}