gpt4 book ai didi

php - Laravel 重命名列丢失所有数据

转载 作者:可可西里 更新时间:2023-11-01 00:19:01 25 4
gpt4 key购买 nike

我是 Laravel 的新手,我使用 php artisan migrate 命令创建了一个用户表:

    Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('username');
$table->string('email');
$table->string('password');
$table->rememberToken();

});

之后,我只需要将 username 列更改为 first_name,然后按如下方式更改架构:

    Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('first_name');
$table->string('email');
$table->string('password');
$table->rememberToken();
});

如果我再次运行 php artisan migrate 命令,它说 Nothing to migrate,然后我使用了 rollback,我丢失了所有表data.. 如何在不影响我的数据的情况下编辑表结构?我讨厌 Laravel 文档

最佳答案

让我们从您的架构开始。您的表名称是 users。它包含一个名为 username 的列,您希望在不丢失现有数据的情况下将其更改为 first_name。您需要为此更改创建新的迁移。

php artian make:migration rename_columns_to_users_table --table=users

将在您的迁移目录中创建一个新的迁移文件。打开它并像这样更改它:

Schema::table('users', function ($table) {
$table->renameColumn('username', 'first_name');
});

保存然后再次运行

php artisan migrate

您的列名称将立即重命名而不会丢失您的旧数据。希望你现在明白了。

您可以在此处找到更多详细信息:https://laravel.com/docs/5.3/migrations#renaming-columns

关于php - Laravel 重命名列丢失所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40458055/

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