Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel migration add column


Laravel migration add column


Here is a concise answer to the query on how to add a new column to an existing table in a Laravel migration:

To add a new column to an existing table in a Laravel migration, you can use the `Schema::table()` method. First, generate a new migration using the `make:migration` Artisan command:


php artisan make:migration add_paid_to_users_table --table=users

This will create a new migration file in the `database/migrations` directory. In the `up()` method of the migration, use the `$table->addColumn()` method to add the new column:

php
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->integer('paid')->after('name');
    });
}

The `after()` method specifies that the new `paid` column should be added after the `name` column.

To remove the column later, you can use the `$table->dropColumn()` method in the `down()` method:

php
public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('paid');
    });
}

Finally, run the migration to apply the changes to the database:


php artisan migrate

This will add the new `paid` column to the `users` table.[1][2][3]

Citations:
[1] https://www.edureka.co/community/65622/how-add-new-column-to-existing-table-of-laravel-in-migration
[2] https://stackoverflow.com/questions/16791613/laravel-add-a-new-column-to-existing-table-in-a-migration
[3] https://www.linkedin.com/pulse/how-add-new-column-using-laravel-migration-mahmoud-abd-elhalim
[4] https://laravel.com/docs/7.x/migrations