In this post, we will focus on the Add new column after another column in our DB table. step by step explain how to add a new column in the migration file. follow the below step to learn how to do it.
will give you a simple example of how to add a column after a specific column in Laravel migration. Alright, let’s dive into the details. for example, we have the "posts" table with columns:
['id', 'title', 'body', 'created_at', 'updated_at'] and we need to add the "status" column after the body column into the table.
Firstly Create a Migration File
php artisan make:migration AddStatusColumnToPostsTable
Then use the after() method from Schema to do it with syntax $table->boolean('status')->after('body');
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->boolean('status')->after('body');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function (Blueprint $table) {
});
}
};
Now, you can run the migration command:
php artisan migrate
You can See The Most Important Software Design Patterns
Thanks
Top comments (0)