How do I re-arrange a column in a Laravel migration file for a MySQL database?
So far I have the following:
$table->date('foo')->after('bar')->change();
However, it does not seem to re-arrange the column.
Why not and how can I fix this?
use Illuminate\Support\Facades\DB; // This goes inside a migration file DB::statement('ALTER TABLE users MODIFY COLUMN external_id VARCHAR(255) AFTER id'); Those of you paying attention would have picked up that we are redefining the column type ( VARCHAR(255) ), it's a little of an annoyance but it's also required.
The Laravel migrations will use the Schema facade to create and modify database tables and columns: Schema::create('tasks', function (Blueprint $table) { $table->bigIncrements('id'); $table->timestamps(); }); Inside the facade, you could specify the different columns that you want to add.
$table->unique('slug');
Can't see anything in Laravel Schema API that will allow you to rearrange columns. Your best bet will be to use raw SQL statement as below.
DB::statement("ALTER TABLE table_name MODIFY COLUMN col_name col_definition AFTER another_col");
Try this, hope it help you to find right solution:
public function up()
{
DB::statement("ALTER TABLE example MODIFY COLUMN foo DATE AFTER bar");
}
public function down()
{
DB::statement("ALTER TABLE example MODIFY COLUMN foo DATE AFTER bar");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With