有时需要在 Laravel 表中添加新列。今天我将向您展示如何在 Laravel 迁移中向现有表添加新列。
想象一下,您有一个名为users的表,其表结构是这样的。
![](https://www.yshuq.com/wp-content/uploads/2022/05/image-17.png)
在迁移并将数据放入表中之后,现在您可能想要在users表中添加一个名为profile
. 要添加新列,刷新数据库并不是一个理想的方式。在这种情况下,您可以创建一个新的迁移文件以将新列添加到users表中。
添加单个列
要在 Laravel 中的现有表中添加新列,您需要在终端中添加以下命令。
php artisan make:migration add_profile_to_users_table
它将add_profile_to_users
在迁移文件夹中创建一个文件。应该是这样的
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddProfileToUsers 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) {
//
});
}
}
现在我编辑它,添加字段“profile”,添加之后的代码应该是这样的。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddProfileToUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
$table->string('profile')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
$table->dropColumn(['profile']);
});
}
}
现在您只需运行以下迁移命令即可在表中添加新列。
php artisan migrate
现在您应该能够在表格中看到一个新列,应该是这样的。
![](https://www.yshuq.com/wp-content/uploads/2022/05/image-18.png)
添加多列
如果需要添加多列,需要在终端中添加如下命令。
php artisan make:migration add_email_and_language_code_and_bio_to_users
它将add_profile_and_bio_to_users
在迁移文件夹中生成一个文件。其中包含以下代码
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddEmailAndLanguageCodeAndBioToUser extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user', function (Blueprint $table) {
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user', function (Blueprint $table) {
//
});
}
}
添加我们的多个字段到代码里面如下:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddEmailAndLanguageCodeAndBioToUser extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user', function (Blueprint $table) {
$table->string('email')->nullable();
$table->string('language_code')->nullable();
$table->string('bio')->nullable();
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user', function (Blueprint $table) {
//
$table->dropColumn(['email','language_code','bio']);
});
}
}
最后,您需要运行以下命令来迁移它。
php artisan migrate
由于上面的执行”php artisan migrate“命令是针对单个文件的操作,所以我们可以在php artisan migrate 后面跟上某个文件的全路径即可。
php artisan migrate --path=./database/migrations/2022_05_18_065750_add_email_and_language_code_and_bio_to_users.php
Migrating: 2022_05_18_065750_add_email_and_language_code_and_bio_to_users
Migrated: 2022_05_18_065750_add_email_and_language_code_and_bio_to_users (42.13ms)
有时候不知道,迁移是否完成处于什么情况,我们可以使用下面命令进行状态查看。
php artisan migrate:status
![](https://www.yshuq.com/wp-content/uploads/2022/05/image-19.png)