Skip to content
Advertisement

Update table structure without rollback in Laravel 5.1

I am beginner in Laravel. I use migrate to create tables in a database, but I don’t want to migrate:rollback to update my table.

I want to apply changes in my old database without loosing current data.

Old migration

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->rememberToken();
        $table->timestamps();
    });
}

New migration

   public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('family');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->rememberToken();
        $table->timestamps();
    });
}

Advertisement

Answer

As far as I know, this is not possible and not the intention of migrations. I think there are two options to solve this:

  1. If you aren’t in production, you can change the migration script and add the additional column manually using mysql:

    alter table users add column family varchar(255);
    
  2. If you need to update a productive database, you should add a second migration script only adding the additional column, e.g.:

    public function up()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->string('family')->after('user_id');
        });
    }
    
    public function down()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->dropColumn('family');
        });
    }
    
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement