Assume that situation, we have migrated our table but suddenly we need a new column and you want to place that specific new field after a column using Laravel migration. In this case, there is a solution. Laravel provides an after()
method to add a column after a column in Laravel migrations.
If you are facing laravel migration add column after not working, then this laravel 9 migration add column after tutorial is for you. So let's see the example code of laravel migration add column after.
Now run this command to add new column after a column:
php artisan make:migration add_slug_column_to_posts_table
Now update this file like:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->string('slug')->unique()->after('id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function (Blueprint $table) {
//
});
}
};
Now if you run php artisan migrate
, it will add a slug
column after the id
column in the posts table.
Read also: Laravel 9 Add New Column In Migration Without Losing Data
Conclusion
I have tried to discuss the clear concept of laravel migration add column after. Now we know laravel 9 migration add column after. Hope this laravel migration add multiple columns after tutorial will help you.