How to add column in a table using laravel 5 migration without losing its data?(如何使用 laravel 5 迁移在表中添加列而不丢失其数据?)
问题描述
我有一个现有的数据库表,我想在其上添加列.但是,当我运行 php artisan migrate
命令时,它没有说明要迁移.但是我已经添加了一个用于添加表列的架构.我已经阅读了一些文章和链接,我应该在添加新列之前首先运行 php artisan migrate:refresh
.问题是,它会删除我表中的现有数据.有什么方法可以在不删除数据的情况下执行迁移并成功在表中添加列?请帮我解决一下这个.非常感谢.这是我的迁移代码.
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');
}
我想在我的 purchase_orders
表中添加列 shipped_via
和 terms
.
I want to add column shipped_via
and terms
in my purchase_orders
table.
推荐答案
使用以下命令修改现有表
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
使用 --create
来创建新表,使用 --table
来修改现有表.
use --create
for creating the new table and --table
for modifying the existing table.
现在将创建一个新的迁移文件.在此文件的 up()
函数中添加这些行
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');
});
然后运行php artisan migrate
这篇关于如何使用 laravel 5 迁移在表中添加列而不丢失其数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 laravel 5 迁移在表中添加列而不丢失其数据?


基础教程推荐
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01