Php 如何通过迁移更改数据类型和添加列而不丢失数据?(拉威尔5.3)
我的迁移是这样的:Php 如何通过迁移更改数据类型和添加列而不丢失数据?(拉威尔5.3),php,mysql,laravel,laravel-5.3,Php,Mysql,Laravel,Laravel 5.3,我的迁移是这样的: public function up() { Schema::create('tests', function (Blueprint $table) { $table->increments('id_test'); $table->string('name', 50); $table->timestamps(); $table->softDeletes(); }); }
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id_test');
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
我想更改数据类型并在表test中添加列。然后更新数据库中的表。我是这样编辑的:
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->string('id_test', 18);
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
然后我运行:php-artisan-migrate:refresh
我的数据丢失在数据库里了
是否有人可以帮助我?使用以下命令创建新的迁移文件:
php artisan make:migration name_of_your_file
它的功能应该是这样的
public function up()
{
Schema::table('tests', function($table) {
$table->renameColumn('id_test', 'id');
$table->string('id_test', 18);
});
}
public function down()
{
Schema::table('tests', function($table) {
$table->dropColumn('id_test');
});
}
现在只需在命令下面运行
php artisan migrate
注意:不要使用refresh
它会删除数据
要了解有关Laravel迁移的更多信息,请参阅:Simple:创建新迁移。不要编辑旧的。在
up()
方法中添加新列,并在down()方法中删除它。然后,只需php artisan migrate
即可运行更改php artisan migrate:rollback
以反转它。您可以通过我需要您的帮助来了解有关迁移的更多信息。看看这个: