A Simple Trick to Update Your Laravel Migrations without creating a new one.

Mohan Sharma
2 min readMar 2, 2020

In Laravel we always encounter a situation where we need to update a migration table, like add new column, drop a column or update a column. And in order to do so you need to create and run a new migration. That is not a big problem until the project grows and very soon we end up with a lot of those unnecessary migration files. In this article I am gonna talk about a trick that I use to eliminate the need of those extra migration classes.

So let’s say that we have created a post migration,

php artisan make:migration create_posts_table

It generates a migration with the name 2019_07_11_1791_create_posts_table

Here is the definition of the migration

public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->timestamps();
});
}

Now later we found out that we need to add a new column body to store the description of the post and a column published to check if the post is published or not.

Now instead of creating a new migration for adding those columns, you can follow these steps:-

First go to your database and find and delete the posts table

Then search for a table called migrations in the database. This table stores information about all the migrations that were generated whenever you run the php artisan migrate command.

Now in this table search for the row that contains 2019_07_11_1791_create_posts_table migration and delete that row from the table.

Now the next time we run php artisan migrate it will regenerate out posts migration

Now we can update our old posts migration class instead of creating a new one.

public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('body');
$table->timestamp('published');
$table->timestamps();
});
}

after that just run php artisan migrate and our new updated posts table will be created in the database

I hope that it was helpful to you. Thank for reading.

--

--

Mohan Sharma

I am a Web Developer and a Freelancer who enjoys his work.