I am trying to rename a column using Laravel migration:
$table->renameColumn('name', 'firstname');
Also, I want to add another column after the firstname
has been added:
$table->renameColumn('name', 'firstname');
$table->string('middlename', 255)->after('firstname')->nullable();
But I am getting an error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'firstname' in <TABLE_NAME> (SQL: alter table <TABLE_NAME> add `middlename` varchar(255) null after `firstname`)
How can I wait for the firstname
column to be added before I add another column after it?
CodePudding user response:
Create a new migration and rename your firstname column and add a new column in that
CodePudding user response:
Calling Schema
two times in a single migration file may solve the issue.
First:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'firstname');
});
Second:
Schema::table('users', function (Blueprint $table) {
$table->string('middlename', 255)->after('firstname')->nullable();
});
Sample Migration:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'firstname');
});
Schema::table('users', function (Blueprint $table) {
$table->string('middlename', 255)->after('firstname')->nullable();
});