usersとcompaniesを、belongsという中間テーブルを介してつなぐ時
中間テーブル上の外部キー以外のカラムも取り出せるようにしたい。
"member_status"もついでに取り出したい。
belongs
| ID | user_id | company_id | member_status |
|---|---|---|---|
| 1 | 12 | 2 | 2 |
| 2 | 4 | 7 | 2 |
| 3 | 4 | 65 | 1 |
| : | : | : | : |
Company.php
public function members()
{
return $this->belongsToMany(User::class, 'belongs')
->withPivot(['member_status']);
}
以下の記述で、取り出すことが出来る。
blade.php
{{ $user->pivot->member_status }}