此方法将会帮助您编写更短的迁移,并使代码更具可读性。
在我们的例子中,我使用非常常见的 user_id,它可以应用于任何外键。
以前,我们会这样写:
$table->unsignedBigInteger('user_id')
->index();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');嗯,有点冗长,对吧?
好消息是我们现在可以这样写 :
$table->foreignId('user_id')
->index()
->constrained()
->onDelete('cascade');