php - 如何在 Laravel 5 中创建只向现有表添加列的迁移

标签 php mysql laravel migration artisan-migrate

我正在尝试向 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();
});

在上面我添加了avatarfirst_namelast_namenickname 列。

我的问题是,当我运行 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/

相关文章:

php - 即使数据库没有内容也无法删除它

php - 使用 Laravel 的 eloquent 在 JSON 列中搜索

php - 'user_id' 在 Laravel 5.4 中没有默认值错误

c# - .Net中如何连接mysql数据库

php - 创建 MySQL 临时表并在 PHP 中从中选择

PHP将文件解析为Array of Array

php - 为在 WooCommerce 中显示自定义产品价格的功能启用回价后缀

php - mysql 中的数据透视表

Laravel 5.7 无法解析 POST 多部分/表单数据请求

php - 访问同一页面 HTML 中的 PHP 变量