Simple question: I'm new to Laravel. I have this migration file:

Schema::create('lists', function(Blueprint $table) {
    $table->increments('id'); 
    $table->string('title', 255);
    $table->integer('user_id')->unsigned(); 
    $table->foreign('user_id')->references('id')->on('users'); 
    $table->timestamps();
});

我想把它更新为增加onDelete('cascade')个.

What's the best way to do this?

推荐答案

首先,你必须将user_id字段作为索引:

$table->index('user_id');

After that you can create a foreign key with an action on cascade:

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

If you want to do that with a new migration, you have to remove the index and foreign key firstly and do everything from scratch.

在down()函数中,您必须执行此操作,然后在up()中执行我在上面编写的内容:

$table->dropForeign('lists_user_id_foreign');
$table->dropIndex('lists_user_id_index');
$table->dropColumn('user_id');

Laravel相关问答推荐

Laravel FAKER语法

(1/1) ErrorException ReflectionFunction::__construct() ex

使用 App::environment() 与 app()->environment() 与 config('app.env') 之间的区别

laravel render() 方法有什么用?

上传时Laravel正在旋转图像

Laravel Artisan Scheduler 中的链式命令?

Laravel 扩展验证自定义消息

laravel 5 中的登录事件处理

PHP 5.5,什么情况下PHP会导致很高的committed memory

Laravel 5.4 - 注册后禁用自动登录

Heroku CLI 安装错误:PATH 未更新,原始长度 1585 > 1024

Laravel - 更新时禁用更新时间

在Lumen框架中启用会话

Laravel5如何将数组传递给查看

laravel 5.1 在没有 VM 重启的情况下看不到作业(job)文件的更改

Laravel 5 - 判断日期是否是今天

如何使 Laravel (Blade) 文本字段只读

Laravel s3 多桶

如何在不要求用户登录 Laravel 的情况下验证Electron邮件

Laravel 中的多对多关系:belongsToMany() 与 hasManyThrough()