【问题标题】:Laravel I added morphmany SQLSTATE[42S22]: Column not found: 1054 Unknown column 'staff.staff_id' in 'field list'Laravel 我添加了 morphmany SQLSTATE[42S22]: Column not found: 1054 Unknown column 'staff.staff_id' in 'field list'
【发布时间】:2020-12-01 17:56:15
【问题描述】:

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

SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列'staff.staff_id'(SQL:选择users.*、staff.staff_idpivot_staff_idstaff .user_id as pivot_user_id, staff.staff_type as pivot_staff_type, staff.role as pivot_role, @9876543@34@.created_at as @987654336.,@987654336. 987654338@ as pivot_updated_at from users inner join staff on users.id = staff.user_id 其中staff.@9876543647@ in (2) 和 @9865444@987654444 @ = 应用\模型\投资者)

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

【问题讨论】:

    标签: php laravel laravel-5 laravel-4


    【解决方案1】:

    你不关注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, 'staff','staffabels',
                                                       'staffabel_id','staff_id')
                    ->withPivot(['role'])
                    ->withTimestamps();
    }
    
    public function investors()
    {
        return $this->morphedByMany(Investor::class, 'staff','staffabels',
                                             'staff_id','staffable_id');
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 2019-06-26
      相关资源
      最近更新 更多