Eli
Eli

Reputation: 1276

How to add column in a table using laravel 5 migration without losing its data?

I have an existing database table and I want to add column on it. However, as I run the php artisan migrate command, it says nothing to migrate. But I already add a Schema for adding table columns. I have read some articles and links that I should run the php artisan migrate:refresh first before the new columns to be added.The problem is, it will erase my existing data in my table. Is there any way I could perform the migration and successfully add columns in my table without deleting my data? Please help me with this. Thanks a lot. Here is my migration code.

public function up()
{
    //
    Schema::create('purchase_orders', function(Blueprint $table){

        $table->increments('id');
        $table->string('po_code');
        $table->text('purchase_orders');
        $table->float('freight_charge');
        $table->float('overall_total');
        $table->timestamps();

    });

    Schema::table('purchase_orders', function(Blueprint $table){
        $table->string('shipped_via');
        $table->string('terms');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
    Schema::drop('purchase_orders');
}

I want to add column shipped_via and terms in my purchase_orders table.

Upvotes: 18

Views: 25796

Answers (3)

Subham Sharma
Subham Sharma

Reputation: 31

To add columns on Database table, you need to add

    php artisan make:migration add_column_name_on_table_name_table --table=table_name

In your case,

    php artisan make:migration add_shipped_via_on_purchase_orders --table=purchase_orders

After that run the command:

    php artisan migrate

Now, you will get the new migration file and you can add column on your migration file.

public function up()
{
    //
    Schema::table('purchase_orders', function(Blueprint $table){
        $table->string('shipped_via');
        $table->string('terms');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    $table->dropColumn('shipped_via');
    $table->dropColumn('terms');

}

Upvotes: 0

Abhishek
Abhishek

Reputation: 3967

Use below command to modify the existing table

php artisan make:migration add_shipped_via_and_terms_colums_to_purchase_orders_table --table=purchase_orders

use --create for creating the new table and --table for modifying the existing table.

Now a new migration file will be created. Inside the up() function in this file add these line

Schema::table('purchase_orders', function(Blueprint $table){
    $table->string('shipped_via');
    $table->string('terms');
});

And then run php artisan migrate

Upvotes: 36

Jirennor
Jirennor

Reputation: 1289

Laravel has a table in your database where it keeps track of all the migrations that are already executed. So by only changing the migration file Laravel will not automatically rerun that migration for you. Cause the migration is already executed by Laravel.

So the best thing to do is to just create a new migration and put the piece of code in it you already have (you were on the right track!).

public function up()
{
    //
    Schema::table('purchase_orders', function(Blueprint $table){
        $table->string('shipped_via');
        $table->string('terms');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //

}

You don't need to populate the down function case the table will be dropped by your current purchase_orders migration.

To migrate the new migration just run:

php artisan migrate

Upvotes: 4

Related Questions