LoginSignup
1
1

More than 5 years have passed since last update.

【Laravel5.1】 updateExistingPivotの使い方

Posted at

Laravelのドキュメントには

 User::find(1)->roles()->updateExistingPivot($roleId, $attributes);

としか書いておらず、$roleIdとはなんぞやと調べたのでメモ。
結論からいうと、中間テーブルのIDだった。
イメージでいうと、

            foreach ($user->roles as $role) {
                $pivot_id = $role->pivot->id;
                $user->roles()->updateExistingPivot($pivot_id, ['role_id' => 5]);
            }

のような感じ。
中間テーブルのFKを更新するのには向かない気がする。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1