Laravel 我在 'field list' 中添加了 morphmany SQLSTATE[42S22]:未找到列:1054 未知列 'staff.staff_id'

Laravel I added morphmany SQLSTATE[42S22]: Column not found: 1054 Unknown column 'staff.staff_id' in 'field list'

我正确地存储了数据,但是当我尝试在编辑功能中获取数据时,它显示了这个 错误

SQLSTATE[42S22]:未找到列:1054 'field list' 中的未知列 'staff.staff_id'(SQL:select users .*, staff.staff_id 作为 pivot_staff_id, staff.user_id 作为 pivot_user_id, staff.staff_type作为 pivot_staff_typestaff.role 作为 pivot_rolestaff.created_at 作为 pivot_created_atstaff.updated_at as pivot_updated_at from users inner join staff on users.id = staff.user_id where [= (2) 中的 15=].staff_idstaff.staff_type = App\Models\Investor)

//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');
    }

你没有关注naming conventions所以你应该确定foreign key"other key" 关系:

 public function staff()
{
    return $this->morphToMany(User::class, 'staff','name of table',
                                                   'foreignPivotKey','relatedPivotKey);
}

我不知道您的 table 列名称,但在您的情况下可能如下所示:

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');
}