Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rotaercz's avatar

How can I add a column via Migration without having to drop the table?

How can I add a column via Migrations without having to drop the table? Basically I'd like to keep the data in my table and just add an additional column via Migrations. Let's say I have an older table that I'd like to add a column to. I can't just rollback the whole database because I want to keep the data in the tables.

How would I go about doing this?

0 likes
5 replies
rotaercz's avatar

I tried adding the column to the migration that I want updated and did:

php artisan migrate

It just outputs,

Nothing to migrate.
thomaskim's avatar
Level 41

You're not supposed to add it to the old migration file. You need to create a new migration file, and then add the corresponding code:

// This gets the users table and adds an email column
Schema::table('users', function (Blueprint $table) {
    $table->string('email');
});

Changes you make to the database should be a new migration file. If you come back in the future and want to alter a column or add another table, that needs to be another migration file. If you decide to drop a table because it's no longer needed, then that needs to be another migration file. Think of each migration as sort of like a snapshot of what your database looked like during that time.

From the docs:

Migrations are like version control for your database, allowing your team to easily modify and share the application's database schema. Migrations are typically paired with Laravel's schema builder to easily build your application's database schema. If you have ever had to tell a teammate to manually add a column to their local database schema, you've faced the problem that database migrations solve.

1 like
rotaercz's avatar

Ah, that clarifies everything. I'll go try it out.

rotaercz's avatar

That worked!

I have two questions.

  1. In the migration file how can I make it so it adds the column after a specific column?

  2. How should the down() function look like to remove the column?

EDIT: Nevermind, the documentation has everything I need. Thanks!

1 like

Please or to participate in this conversation.