在 Laravel 中使用迁移更新现有表的列而不丢失数据
在开始编写代码之前,我们需要了解Laravel 迁移以及它如何帮助我们。
首先,如果我们想构建一个项目,就需要先设计数据库。那么我们该怎么做呢?我们可以直接设计数据库。但是,如果漏掉了一些字段/列或数据类型,就需要直接在数据库中进行修正,这在 Laravel 中是一种不好的做法。
在另一个场景中,假设你有一个项目,需要将其安装到你朋友的设备上,你可能需要发送 SQL 文件;这很麻烦。
现在想象一下,如果你有这样的功能:拉取项目并运行一个命令,数据库就立刻设置好了。是不是很方便?
Laravel 的迁移功能也是如此。你无需处理 SQL 文件,也不用担心数据类型错误或拼写错误,只需在代码中修复即可。你无需直接操作数据库,只需在代码中修复,然后运行 ` php artisan migrate:refresh`即可。希望你已经体验到了 Laravel 迁移的种种好处。
现在我们来看另一个场景。
有时,当我们需要添加或修改现有数据库表时,我们会非常担心。
例如,我们有一个用户模型和一个用户迁移表,其中包含姓名、用户名、已验证的电子邮件和密码。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
但是项目部署后,随着用户数量的增加,我们发现需要在表中添加一个新字段,例如电话号码。另外,我们还需要保留现有数据。
因此,我们将创建一个新的迁移文件,以便在现有表中添加新列。
现在使用以下命令:
php artisan make:migration add_phone_number_to_users_table
这里我们需要确保表名与新的迁移文件匹配,就像我们在两个地方都使用users一样。
使用该命令后,我们可以看到类似这样的新迁移文件:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddPhoneNumberToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
}
现在您可以在此迁移文件中添加所需的字段。因为我们需要一个电话号码字段。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddPhoneNumberToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('phone_number')->unique()->after('email');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
}
这里我们在`up()`方法中,在`email`之后添加了`phone_number`字段。你可以使用 `<column> ` 或 `<column>`将该列放置在任何你想要的位置。你可以从Laravel 文档的“列修改器”部分了解更多修改器信息。after()before()
现在我们离完成任务还差一步。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddPhoneNumberToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('phone_number')->unique()->after('email');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('phone_number');
});
}
}
这里可以看到,我们添加了一些内容$table->dropColumn('phone_number');,这样如果运行回滚命令,它就能正常工作。如果您有很多字段,可以使用类似这样的 dropcolumn 数组。$table->dropColumn(['phone_number']);
最后,我们开始吧……
现在你需要运行以下命令:
php artisan migrate
我们完成任务啦! :D
您可以访问Laravel 迁移文档了解更多迁移选项。
希望对您有所帮助。
文章来源:https://dev.to/mahmudulhsn/update-existing-table-with-migration-without-losing-in-data-in-laravel-fb1