将新列添加到 table

Add new column to table

作为一名开发人员,我还是新手,我正在尝试更改用户 table,我是否从之前的 table 添加了一个新列 "user_type"。

我的问题是:我可以这样做还是应该这样做 php artisan make:migration add_user_type_users ?

因此,如果我可以在不使用 add_user_type_users 的情况下更改主 table,我应该如何将其迁移到 laravel forge 才能看到更改。因为,当我将更改提交给 forge 时,它​​不会添加我的新列 user_type.

新信息

如何将更改提交到实时数据库中?我正在使用 Laravel Forge 和 DigitalOcean

(编辑:回答原始问题)

如果您需要向现有 table 添加新闻栏目,您必须:

  1. 创建一个新的迁移文件:php artisan make:migration <fileName>
  2. 在迁移文件中使用 Schema::table() 而不是 Schema::create()
  3. 像普通迁移一样添加列
  4. 运行 迁移:php artisan migrate

示例:

//change create for table
//the first param (in this case 'users') must be the table name that you want to add columns
Schema::table('users', function (Blueprint $table) {
        //news colums here
        $table->string('new_col');
    });

编辑: 回答编辑过的问题:

Can I do that or should I do the php artisan make:migration add_user_type_users ?

-

可行但不是正确的方法:

是的,您可以编辑您的旧迁移文件,但是如果您这样做,您的下一个 php artisan migrate 将不会更改已经迁移的文件,并且您必须在此处删除您的 table 和他的数据在这种情况下,并从 migrations table 中删除 运行 您编辑的迁移文件。所以这不是生产环境的答案。如果你这样做,你会遇到很多你会避免的问题。 也许你可以在开发中这样做。环境。因为您可以随时删除和创建 tables 而不会丢失真实数据。

-

正确的方法:

你必须创建新的迁移文件(就像原来的答案),你不会有丢失数据的问题,(除非你想要这个)并且迁移将 运行 在生产中不删除任何东西。