gpt4 book ai didi

php - Laravel::更新外键的最佳方式

转载 作者:IT王子 更新时间:2023-10-28 23:52:05 25 4
gpt4 key购买 nike

我有这个迁移文件

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');

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

执行此操作的最佳方法是什么?

有没有像->change();

这样的东西

谢谢

最佳答案

删除外键然后再次添加并运行迁移。

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');
});
}

关于php - Laravel::更新外键的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38427672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com