我是Laravel的新手,并且使用以下php artisan migrate命令创建了一个users表:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('username');
$table->string('email');
$table->string('password');
$table->rememberToken();
});
Run Code Online (Sandbox Code Playgroud)
之后,我只需要更改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();
});
Run Code Online (Sandbox Code Playgroud)
如果php artisan migrate再次运行该命令,它显示为Nothing to migrate,那么我使用rollback,并且所有表数据都丢失了。如何在不影响数据的情况下编辑表结构?我讨厌Laravel文档
让我们从您的模式开始。您的表格名称是users。它包含一个名为的列username,您希望将其更改为first_name不丢失现有数据的列。您需要为此更改创建一个新的迁移。
php artian make:migration rename_columns_to_users_table --table=users
Run Code Online (Sandbox Code Playgroud)
将在您的迁移目录中创建一个新的迁移文件。打开它并像这样更改它:
Schema::table('users', function ($table) {
$table->renameColumn('username', 'first_name');
});
Run Code Online (Sandbox Code Playgroud)
保存并再次运行
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
您的列名将立即重命名,而不会丢失您的旧数据。希望你现在明白了。
您可以在这里找到更多详细信息:https : //laravel.com/docs/5.3/migrations#renaming-columns
| 归档时间: |
|
| 查看次数: |
570 次 |
| 最近记录: |