Answer the question
In order to leave comments, you need to log in
How to setup relationship with extra field in povit table?
Hello!
Can anyone tell me how to properly set up the relationship in the pivot table for the additional field?
pivot table:
the department_id column is an add. field
class EduSpeciality extends Model
{
//
public function profiles()
{
return $this->belongsToMany(Profile::class)
->withPivot('department_id');
}
}
dd($this->eduSpeciality->profiles->toArray());
$this->eduSpeciality->profiles->first()->pivot->department->name
Answer the question
In order to leave comments, you need to log in
your situation is theoretically solved in this way:
https://laravel.ru/docs/v5/eloquent-relationships#hmt
but as far as I understand, the pivot table should have two foreign keys
, that is, if you have three of them, then you need to do two intermediate tables
, well, if it's wise)))
ZY . That is, withPivot(), as far as I understand, serves to manipulate auxiliary fields, such as updated_at, and not to create a pivot table with many foreign keys.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question