gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:10:55 24 4
gpt4 key购买 nike

我有一个现有的数据库表,我想在上面添加列。但是,当我运行 php artisan migrate 命令时,它没有说要迁移。但是我已经添加了一个用于添加表列的架构。我已经阅读了一些文章和链接,我应该在添加新列之前先运行 php artisan migrate:refresh。问题是,它会删除我表中的现有数据。有什么方法可以在不删除数据的情况下执行迁移并在表中成功添加列?请帮我解决一下这个。非常感谢。这是我的迁移代码。

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 表中添加列 shipped_viaterms

最佳答案

使用以下命令修改现有表

php artisan make:migration add_shipped_via_and_terms_colums_to_purchase_orders_table --table=purchase_orders

使用 --create 来创建新表,使用 --table 来修改现有表。

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

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

然后运行php artisan migrate

关于php - 如何使用 laravel 5 迁移在表中添加列而不丢失其数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36718773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com