DEV Community

Cover image for Laravel 9 - Create Column after Another Column
Sandro Jhuliano Cagara
Sandro Jhuliano Cagara

Posted on

4 3

Laravel 9 - Create Column after Another Column

Notice: Only for MySQL.

If you're adding a new column to the existing table, it doesn't necessarily have to become the last in the list. You can specify after which column it should be created:

Schema::table('table', function (Blueprint $table) {
    $table->string('column')->after('column');
});
Enter fullscreen mode Exit fullscreen mode

If you're adding a new column to the existing table, it doesn't necessarily have to become the last in the list. You can specify before which column it should be created:

Schema::table('table', function (Blueprint $table) {
    $table->string('column')->before('column_2');
});
Enter fullscreen mode Exit fullscreen mode

If you want your column to be the first in your table , then use the first method.

Schema::table('table', function (Blueprint $table) {
    $table->string('column')->first();
});
Enter fullscreen mode Exit fullscreen mode

Also the after() method can now be used to add multiple fields.

Schema::table('table', function (Blueprint $table) {
    $table->after('remember_token', function ($table){
        $table->string('column_1')->nullable();
        $table->string('column_2')->nullable();
    });
});
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay