添加新列后出现数据库错误

时间:2017-04-12 12:06:38

标签: mysql laravel laravel-migrations

我创建了一个登录和注册表单。但是我忘了添加一个专栏。所以,我想添加一个名为“contactNumber”的新列。为了添加列,我在cmd.exe中编写了一个名为php artisan migrate:refresh的命令,并在迁移文件夹中的create_users_table中编写了一个代码。但是,新列的值不会转到mysql数据库。我做错了什么?

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:1)

您需要将新列添加到模型中,因此在User类中需要执行此操作:

protected $fillable = [
    'name', 'email', 'password', 'contactNumber'
];

希望这个帮助

答案 1 :(得分:1)

您尝试在数据库中插入新记录,但contactNumber列没有默认值。在迁移文件中,您可以将nullable添加到该列,例如。

$table->string('contactNumber')->nullable();

或直接在GUI中的数据库中。

答案 2 :(得分:0)

对于这种类型的场景,我个人创建了这样的新迁移

php artisan migrate:make add_column_to_table --table="tableName"

现在运行新迁移运行此命令。

php artisan migrate

这将在您的表中添加新列。还要查看此主题。 https://laracasts.com/discuss/channels/laravel/add-new-column-to-existing-table

相关问题