I store the data correctly but when I try to get a data in edit function it shows me this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘staff.staff_id’ in ‘field list’ (SQL: select users.*, staff.staff_id as pivot_staff_id, staff.user_id as pivot_user_id, staff.staff_type as pivot_staff_type, staff.role as pivot_role, staff.created_at as pivot_created_at, staff.updated_at as pivot_updated_at from users inner join staff on users.id = staff.user_id where staff.staff_id in (2) and staff.staff_type = AppModelsInvestor)
//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');
}
Advertisement
Answer
you don’t follow naming conventions so you should determine the foreign key and "other key" for the relationship:
public function staff()
{
return $this->morphToMany(User::class, 'staff','name of table',
'foreignPivotKey','relatedPivotKey);
}
I don’t know your table columns name but in your case can be something like below:
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');
}