Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

moses's avatar
Level 2

How to change data type and add column with migration without losing data? (laravel 5.3)

My migration is like this :

public function up()
{
    Schema::create('tests', function (Blueprint $table) {
        $table->increments('id_test');
        $table->string('name', 50);
        $table->timestamps();
        $table->softDeletes();
    });
}

public function down()
{
    Schema::drop('tests');
}

I want to change data type and add column in table test. Then update table in database. I edit like this :

public function up()
{
    Schema::create('tests', function (Blueprint $table) {
        $table->increments('id');
        $table->string('id_test', 18);
        $table->string('name', 50);
        $table->timestamps();
        $table->softDeletes();
    });
}

public function down()
{
    Schema::drop('tests');
}

Then I run : php artisan migrate:refresh

I lost my data in database

Is there any people who can help me?

0 likes
2 replies
naresh-io's avatar

@moses If you want to add more columns or want to change the existing columns, just create another migration to alter table and run php artisan migrate not php artisan migrate:refresh

1 like

Please or to participate in this conversation.