生产迁移新鲜

How does it work if I want to add a new column in my production table? I know that I will loose all the data when I make a migration:fresh.

1) is it possible to change the migration and add a new column as I did with the fastnetnr column?

 public function up()
    {
        Schema::create('kontaktforms', function (Blueprint $table) {
            $table->increments('id');
            $table->string('navn');
            $table->string('mobilnr');
            //new fastnetnr column added
            $table->string('fastnetnr')->nullable();
            $table->string('mail');
            $table->string('emne');
            $table->text('beskrivelse');
            $table->timestamps();
        });

2) Or do I have to add a new column with php artisan so the output is like this?

public function up()
{
    Schema::table('kontaktforms', function($table) {
        $table->string('fastnetnr')->nullable();
    });
}

</div>

Logically you can do it by following steps;

  1. rollback your table php artisan migrate:rollback or php artisan migrate:rollback --step=1
  2. edit your migration file
  3. migration back php artisan migrate

but it's not a best practice, especially in production. so you have to create new migration file for modify table and migrate it.

create new migration file

php artisan make:migration add_fastnetnr_to_kontaktforms_table --table=kontaktforms

and add new column like this

public function up()
{
    Schema::table('kontaktforms', function (Blueprint $table) {
        $table->string('fastnetnr')->nullable();
    });
}


public function down()
{
    Schema::table('kontaktforms', function (Blueprint $table) {
        $table->dropColumn('fastnetnr');
    });
}

and migrate

php artisan migrate