In Laravel development, we often use migration to manage our database structure. But what should we do when we need to delete a migration?
In this article, we will discuss several ways to remove migration in Laravel.
Method 1: migrate:rollback
Laravel provides a very simple method to rollback migration, use the following command:
php artisan migrate:rollback
This will rollback our last execution migration.
But what should we do if we want to delete a specific migration? In this case, we can try using the following command:
php artisan migrate:rollback --step=1
This will roll back the number of steps of the last migration we performed. These steps can be changed with the --step=
option. For example, if we want to roll back 3 migrations, we can run the following command:
php artisan migrate:rollback --step=3
It should be noted that rolling back a migration will delete the data table corresponding to the migration in the database. If there is data in the table, then The data will also be deleted.
Method 2: migration:rollback
Use the following command to roll back the migration:
php artisan migrate:rollback --path=/database/migrations/{specific_migration_file_name.php}
In the curly brackets, use the specific migration file name instead. This will roll back the specified migration and delete the table corresponding to the migration from the database.
It should be noted that rolling back a migration will delete the data table corresponding to the migration in the database. If there is data in the table, the data will also be deleted.
Method 3: Delete the migration file and pass
We can delete the migration file directly, but before doing this, we need to delete the migration file record in the migration table. Otherwise, we will not be able to run the migrate command again.
We can delete the migration file records from the migration table using the following command:
php artisan migrate:reset
This will delete all records in the migration table from the database so that we can rerun the migrate command.
Now, we can delete the specific migration file and pass. However, we need to pay attention to the following points:
Of course, deleting the migration file and passing is the least recommended method. Because it cannot be rolled back and exceptions are prone to occur.
Summary:
None of these methods are perfect, they can be chosen according to the environment and needs. Projects based on compressed packages only need to delete a certain migration file; in an online environment, the difference between rolling back and deleting a specified migration requires developers to have a certain understanding of the migration table before operating. In any case, we need to operate with caution and make data backups for deletion operations.
The above is the detailed content of laravel delete migration. For more information, please follow other related articles on the PHP Chinese website!