doutu4335 2018-11-19 19:04
浏览 37
已采纳

如何通过迁移在laravel 5.7中创建非空列?

How can I create non-null columns in laravel 5.7 by migrating?

$table->string('title','50')->nullable();

does not work.
What can I do?

  • 写回答

1条回答 默认 最新

  • doudian6229 2018-11-19 19:11
    关注

    When you migrate to add a column to a table that already has records, you will need to make it nullable, or at least give it a default. Otherwise what would the value be for all of your current records?

    Worst case scenario if you have no default value, make it nullable. Then go through and set the value for each one, then remove the nullable.

    Your second migration (to remove nullability) would be like this:

    $table->string('title','50')->nullable(false)->change();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?