使用Laravel的迁移从单个配置文件管理数据库模式

时间:2016-09-06 20:18:43

标签: php laravel laravel-5 migration database-schema

我想使用Laravel的迁移来管理我的数据库。但我 需要 才能拥有一个存储架构的配置文件。像这样:

        {
           user: {
                    "id":"increments",
                    "name":"string",
                    "timestamps":"timestamps()"
                 }
        }

当我再次将此文件更改为下面的文本时

        {
           user: {
                    "id":"increments",
                    "name":"string",
                    "password":"string",
                    "timestamps":"timestamps()"
                 }
        }

我希望能够运行命令并更改数据库,而不会丢失任何数据或创建其他配置文件。

我是否可以使用laravel迁移实现此目的,或者如果您知道任何其他可以帮助我的解决方案,并且我可以在laravel上使用它而不会丢失任何laravel的数据库管理工具,请发表评论。 谢谢。

1 个答案:

答案 0 :(得分:0)

php artisan make:migration add_password_to_user(你确定这个表叫做用户而不是用户吗?)

在新的迁移中

public function up()
{
    Schema::table('user', function($table) {
        $table->string('password');
    });
}

不要忘记回滚

public function down()
{
    Schema::table('user', function($table) {
        $table->dropColumn('password');
    });
}

如果您的模型用户,则表格应为用户 ..否则请确保模型上有protected $table = 'user';

https://laravel.com/docs/5.3/migrations

--table和--create选项也可用于指示表的名称以及迁移是否将创建新表。这些选项只是使用指定的表预填充生成的迁移存根文件:

php artisan make:migration create_users_table --create = users

php artisan make:migration add_votes_to_users_table --table = users

相关问题