我正在尝试使用Laravel迁移重命名列:
$table->renameColumn('name', 'firstname');
此外,我想在添加firstname
之后添加另一列:
$table->renameColumn('name', 'firstname');
$table->string('middlename', 255)->after('firstname')->nullable();
但我得到了一个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'firstname' in <TABLE_NAME> (SQL: alter table <TABLE_NAME> add `middlename` varchar(255) null after `firstname`)
如何等待添加firstname
列,然后再添加另一列?
在单个迁移文件中调用Schema
两次可以解决此问题。
第一:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'firstname');
});
第二:
Schema::table('users', function (Blueprint $table) {
$table->string('middlename', 255)->after('firstname')->nullable();
});
样本迁移:
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'firstname');
});
Schema::table('users', function (Blueprint $table) {
$table->string('middlename', 255)->after('firstname')->nullable();
});