php – 如何使用laravel 5迁移在表中添加列而不丢失其数据?

我有一个现有的数据库表,我想在其上添加列.但是,当我运行PHP artisan migrate命令时,它没有说任何迁移.但是我已经添加一个用于添加表列的Schema.我已经阅读了一些文章链接,我应该运行PHP artisan migrate:在添加新列之前先刷新.问题是,它将擦除我表中的现有数据.有没有什么办法可以执行迁移并在我的表中成功添加列而不删除我的数据?请帮我解决一下这个.非常感谢.这是我的迁移代码.

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');
}

我想在purchase_orders表中添加列shipping_via和条款.

解决方法:

使用以下命令修改现有表

PHP artisan make:migration add_shipped_via_and_terms_colums_to_purchase_orders_table --table=purchase_orders

use –create用于创建新表, – table用于修改现有表.

现在将创建一个新的迁移文件.在此文件中的up()函数添加这些行

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

然后运行PHP artisan migrate

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...