Forem

TRUNG VU
TRUNG VU

Posted on

1 1

Add multiple columns after a column in Laravel Migrations

From Laravel 8.27 you can add multiple new columns after an existing column at the same time.

In previously, it isn’t difficult to write, you’d have to call after() method for each subsequent new column.

Schema::table('customers', function ($table) {
    $table->string('address_line1')->after('password');
    $table->string('address_line2')->after('address_line1');
    $table->string('city')->after('address_line2');
});
Enter fullscreen mode Exit fullscreen mode

From version 8.27 you can group them in a new after() method on the Blueprint migration instance.

Schema::table('customers', function ($table) {
    $table->after('password', function ($table) {
        $table->string('address_line1');
        $table->string('address_line2');
        $table->string('city');
    });
});
Enter fullscreen mode Exit fullscreen mode

Happy Coding:)

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay