In Laravel migrations, there are commands that are helpful. The commands below are used to run operations on the database.
php artisan migrate:rollbackphp artisan migrate:resetphp artisan migrate:refreshphp artisan migrate:fresh
I will explain how to use the commands.
php artisan migrate:rollback
First, let's check which database migrations we've already run.
You can run php artisan migrate:status
, and the output would give you a result like this:
php artisan migrate:status Migration name ...................................................... Batch / Status2014_10_12_000000_create_users_table ....................................... [1] Ran2014_10_12_100000_create_password_reset_tokens_table ....................... [1] Ran2019_08_19_000000_create_failed_jobs_table ................................. [1] Ran2019_12_14_000001_create_personal_access_tokens_table ...................... [1] Ran2023_11_05_190006_create_posts_table ....................................... [2] Ran
The php artisan migrate:rollback
command rolls back the last batch of migrations, which may include multiple migration files. In this example, before this command, it only rolls back the 2023_11_05_190006_create_posts_table
.
Moreover, we can specify what steps we want to rollback. For example, specifying the parameter --step=2
tells Laravel to only rollback the last two migrations.
php artisan migrate:reset
The php artisan migrate:reset
command will roll back all of your application migrations. Basically, this command will run the down method of all migrations.
php artisan migrate:refresh
The php artisan migrate:refresh
command is used to roll back all the migrations and re-run all of your migrations.
php artisan migrate:fresh
There are situations where the migrate:refresh
command isn't helpful. The migrate:fresh
command will drop all the tables and run the migration command.