外部キー制約を更新する


Laravelのマイグレーションで外部キー制約を更新する場合は、一旦削除して、再度作り直します。

例えば、usersテーブルのgroupsテーブルに対する外部キーgroup_idの制約をCASCADEに変更するには、次のような記述になります。

Schema::table('users', function (Blueprint $table) {
    $table->dropForeign('users_group_id_foreign');

    $table->foreign('group_id')
        ->references('id')
        ->on('groups')
        ->onDelete('CASCADE')
        ->onUpdate('CASCADE');
});