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

前端 未结 2 1389
醉话见心
醉话见心 2020-12-24 14:17

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 a

2条回答
  •  臣服心动
    2020-12-24 14:44

    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
    

提交回复
热议问题