Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在重命名列名后在laravel中迁移而不丢失数据?_Laravel_Migration - Fatal编程技术网

如何在重命名列名后在laravel中迁移而不丢失数据?

如何在重命名列名后在laravel中迁移而不丢失数据?,laravel,migration,Laravel,Migration,场景:我创建了一个包含大量数据的列名price,现在我需要将该“price”列名更改为oldPrice,而不会丢失数据并使用迁移。 有什么办法请告诉我。感谢您执行或撤消迁移中的任何更改。你必须编写新的迁移,这不会让你或你的队友丢失任何数据,你的应用程序也不会崩溃 要更改列,请使用以下命令: 迁移 php artisan make:migration change_price_to_old_price --table=table_name 并根据您的要求使用以下代码 在up()函数中: publ

场景:我创建了一个包含大量数据的列名
price
,现在我需要将该“price”列名更改为oldPrice,而不会丢失数据并使用迁移。
有什么办法请告诉我。感谢您执行或撤消迁移中的任何更改。你必须编写新的迁移,这不会让你或你的队友丢失任何数据,你的应用程序也不会崩溃

要更改列,请使用以下命令:

迁移

php artisan make:migration change_price_to_old_price --table=table_name
并根据您的要求使用以下代码

在up()函数中:

public function up(){
   Schema::table('table_name', function (Blueprint $table) {
       $table->renameColumn('old_name', 'new_name');
   });
}
在down()函数中只需撤消在
up()中所做的操作

推荐: 始终向下写入
迁移,以撤消在
向上
函数中正在执行的操作。 php artisan migrate:status查看哪些迁移已运行,哪些仍在运行


点击php artisan migrate:查看命令列表并了解它们

OK,但我需要迁移来运行此迁移,对吗?我猜这会刷新所有的数据。一点也不。上面的命令将在
数据库目录中创建一个新文件
,在那里您将编写代码来重命名您的列。当您运行
php artisan migrate
时,将运行最新的迁移,它只会更改列名,并且您的数据将在那里,而不会出现单个change@SandeshPoudel如果它解决了你的问题,请告诉我problem@AfrazAhmad如果您包含一个示例
down()可以吗
答案中的方法。@OluwatobiSamuelOmisakin我已更新了我的答案您到目前为止尝试了什么?
public function down(){

   Schema::table('table_name', function (Blueprint $table) {
       $table->renameColumn('new_name', 'old_name');
   });
}