Laravel ::更新外键的最佳方法 [英] Laravel :: Best way to update a foreign key

查看:228
本文介绍了Laravel ::更新外键的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此迁移文件

Schema::create('table_one', function(Blueprint $table) 
{ 
    $table->increments('id'); 
    $table->string('name'); 
    $table->integer('table_two_id')->unsigned(); 
    $table->foreign('table_two_id')->references('id')->on('table_two'); 
    $table->timestamps(); 
});

,我想进行更新以使其-> onDelete('cascade');

and I want to update to make it ->onDelete('cascade');

$table->foreign('table_two_id')->references('id')->on('table_two')->onDelete('cascade');

做到这一点的最佳方法是什么?

What is the best way to do this?

是否存在类似-> change();

谢谢

推荐答案

删除外键,然后再次添加它并运行迁移.

Drop the foreign key then add it again and run migrate.

public function up()
{
    Schema::table('table_one', function (Blueprint $table) {
        $table->dropForeign(['table_two_id']);

        $table->foreign('table_two_id')
            ->references('id')
            ->on('table_two')
            ->onDelete('cascade');
    });
}

这篇关于Laravel ::更新外键的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆