How to roll back the migration of Laravel

Revert a Laravel migration with an example: learn how to undo a migration and restore the database to its previous state.

Rolling Back Migrations in Laravel

Migrations in Laravel provide an easy way to manage the database structure. They allow developers to easily create and modify tables, columns, and indexes in a database, as well as maintain and rollback the changes. This makes it easier to keep the database structure in sync between different developers and different environments.

In this example, we will be rolling back a migration in Laravel. The first step is to find the migration file you want to roll back. In this example, the migration file is called "create_users_table.php". The file can be found in the "database/migrations" directory.


increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

?>

The next step is to execute the "down" function in the migration file. This function is responsible for reversing the changes made by the "up" function. In this example, the "down" function will drop the "users" table.

To run the "down" function, you can use the Artisan command line tool. Open a terminal window and navigate to the root directory of your Laravel application. Then, run the following command:

php artisan migrate:rollback
This command will execute the "down" function for all of your migrations.

Finally, you can verify the migration was rolled back by checking the database. The "users" table should no longer exist.

Rolling back migrations in Laravel is a simple process. With the help of Artisan, you can easily revert any changes made to your database structure. This makes it easy to keep the database structure in sync between different developers and different environments.

Answers (0)