php - Laravel 我添加了 morphmany SQLSTATE[42S22]: Column not found: 1054 Unknown column 'staff.staff_id' in 'field list'
问题描述
我正确存储了数据,但是当我尝试在编辑功能中获取数据时,它显示了这个错误
SQLSTATE [42S22]:未找到列:1054 '字段列表'中的未知列' staff.staff_id '(SQL:从内部连接中选择. * 、. users
as staff
、staff_id
.as pivot_staff_id
、.as 、 .as 、 .as 、.as . = .其中.在 (2) 和. = App\Models\Investor)staff
user_id
pivot_user_id
staff
staff_type
pivot_staff_type
staff
role
pivot_role
staff
created_at
pivot_created_at
staff
updated_at
pivot_updated_at
users
staff
users
id
staff
user_id
staff
staff_id
staff
staff_type
//investor relationship
public function staff()
{
return $this->morphToMany(User::class, 'staff')
->withPivot(['role'])
->withTimestamps();
}
//user relationship
public function investors()
{
return $this->morphedByMany(Investor::class, 'staff');
}
解决方案
您不遵循命名约定,因此您应该确定关系的foreign key
and "other key"
:
public function staff()
{
return $this->morphToMany(User::class, 'staff','name of table',
'foreignPivotKey','relatedPivotKey);
}
我不知道您的表格列名称,但在您的情况下可能如下所示:
public function staff()
{
return $this->morphToMany(User::class, 'staff','staffabels',
'staffabel_id','staff_id')
->withPivot(['role'])
->withTimestamps();
}
public function investors()
{
return $this->morphedByMany(Investor::class, 'staff','staffabels',
'staff_id','staffable_id');
}
推荐阅读
- reactjs - 在 React 中使用 class 而不是 className 属性的任何或多或少的官方方式
- python - 在后台进程的python子进程中使用'&'
- swift - 如何调整通过UIDatePicker设置的日期
- asp.net - 为什么 _session.Use 方法不能正常工作?
- opengl - 如何在 FreeType 中重置字符的 X 位置
- android-studio - 将 Unity 游戏导入为动态功能
- clojure - 如何在 Clojure EDN 中以不同的顺序执行自定义标签阅读器
- java - Java - 如何将 CSV 文件反序列化为 JavaBeans
- html - 剪辑路径无法正确缩放
- google-cloud-platform - 我正在尝试在谷歌云功能中添加验证 jwt 令牌方法,但它显示错误