我正在尝试向 Laravel 5 安装附带的通用“用户”表中添加一些新列。
但是我创建了一个新的迁移,它执行一个 Schema::create 如下:
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('email')->unique();
$table->string('password', 60);
$table->string('avatar');
$table->string('first_name');
$table->string('last_name');
$table->string('nickname');
$table->rememberToken();
$table->timestamps();
});
在上面我添加了avatar
、first_name
、last_name
和nickname
列。
我的问题是,当我运行 php artisan migrate
时,它告诉我Nothing to migrate.
如何创建“更新”迁移?
最佳答案
来自 http://laravel.com/docs/5.1/migrations#creating-columns
创建列
要更新现有表,我们将在 Schema 外观上使用 table
方法。与 create
方法一样,table
方法接受两个参数:表的名称和一个接收蓝图实例的闭包,我们可以使用它来向表中添加列:
Schema::table('users', function ($table) {
$table->string('email');
});
也可以从http://laravel.com/docs/5.1/migrations#creating-columns看这个
在单个命令中回滚/迁移注意:使用此方法您将丢失数据库中的所有数据
migrate:refresh 命令将首先回滚所有数据库迁移,然后运行 migrate 命令。此命令有效地重新创建整个数据库:
php artisan migrate:refresh
关于php - 如何在 Laravel 5 中创建只向现有表添加列的迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30891346/