向表中添加新列

问题描述 投票:0回答:1

作为开发人员我还是新手,我正在尝试更改用户表,我是否从上一个表中添加了一个新列“user_type”。

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

因此,如果我可以在不使用add_user_type_users的情况下更改主表,那么在看到更改之前,应该如何将其迁移到laravel forge中。因为,当我将更改提交到伪造时,它不会添加我的新列user_type.enter image description here

新信息

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

php laravel database-migration laravel-5.6
1个回答
1
投票

(编辑:回答原始问题)

如果需要向现有表添加新列,则必须:

  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');
    });

编辑:回答编辑问题:

我可以这样做或者我应该做php artisan make:migration add_user_type_users?

-

是可能的,但不是正确的方式:

是的,您可以编辑旧的迁移文件但是如果您这样做,您的nexts php artisan migrate将不会更改已迁移的文件,您在这里必须删除您的表及其数据,并从迁移表中删除以运行您编辑的迁移文件。所以这不是生产环境的答案。如果你做dat,你会遇到很多你会避免的问题。也许你可以在开发者身上这样做。 ENV。因为您可以在每次需要时删除和创建表而不会丢失实际数据。

-

正确的方式:

您必须创建新的迁移文件(就像原始答案一样),并且您不会遇到丢失数据的问题(除非您需要这样),并且迁移将在生产中运行而不删除任何异常。

© www.soinside.com 2019 - 2024. All rights reserved.