This tutorial explains how to rollback migration in Laravel. It focuses on rolling back migrations in Laravel and how to revert them. I would like to share with you the concept of rolling back migrations in Laravel. This tutorial will help you understand how Laravel migration rollback works.
Migrations as a version control system for your database. They allow us to define and share the application’s database schema definations.
I will provide an easy example of how to rollback a database migration using the Laravel migrate command. You can also rollback a specific migration using the ‘step’ argument, and you can rollback all migrations as well.
In addition, we will look at how to rollback migrations using the command line.
Laravel rollback migration
Laravel migration allow you to manage your database schema. Using migration, you can create table, columns, and indexes using PHP code, it easy to manage changes in your database. Laravel also provides a method to “rollback” migrations, undoing changes made by previous migrations.
1. First create migration below command
php artisan make:migaration create_posts_table
php artisan make:migration create_dataitems_table
php artisan make:migration create_productitems_table
Read Also: How to convert JSON to array in Laravel?
2. Rolling back the last migration
Rollback migration in laravel
An easy method to rollback the latest migration is with the ‘migrate::rollback’ command. This command rollback the last batch of migration. If included multiple migration files so can do this by running this command. Multiple method to can do rollback migration laravel.
php artisan migrate:rollback
This will undo the last batch of migrations that were run. All will be rolled back.
Database varification after rollback
After run rollback migrations command, verify the database schema and data to ensure the rollback was successful.
- Checking migration status using below command
php artisan migrate:status
This command return all lists of migrations, showing which ones have been applied.
Note: migrate:rollback can loss data if not used carefully. Ensure you have a backup strategy in place.
3. Rolling back a limited number migrations
If you want to rollback multiple batches, you can use the –step option. For example, to rollback the last two batches, you can run below command.
For instance, we can rollback the last three migrations as see below.
php artisan migrate:rollback --step=3
This will rollback the last three batchs of migrations.
- The ‘php artisan’ command specifies that you want to use Laravel’s built-in command line tool. This tool allow you to run command specifically for your laravel application.
- ‘migrate:rollback’ is the actual command. It says you want to rollback certain database migrations.
- Last part ‘–step=3’, is a parameter for the ‘migrate:rollback’ command.
4. Rolling back all migrations
If you want rollback all migrations, you can use the migrate:reset command. command as below.
php artisan migrate:reset
This command will rollback all migrations, reverting your database schema to its original state before any migrations were applied.
5. Rolling back migrations from a custom path
For instance, if our migrations are located in a different directory, we can use the ‘ –path’ option with the ‘migrate:rollback’ command to rollback specific migrations from that path.
We have to explained the full path to the migration file we want to rollback. Here’s an example.
php artisan migrate:rollback --path=/database/migrations/your-migration-file.php
6. Re-run migrations
If you need to re-run all migrations after rolling them back, you can use the migrate:refresh command. There comes a when you want to make some changes to am existing table. You want to change the data type of a column. So in this situation, you can modify the migration file of the table and them run the migration again using the below command.
php artisan migrate:refresh
7. Creating fresh migrations
When, if we want to start fresh and drop all existing database tables, we can use the ‘migrate:fresh’ command as below.
php artisan migrate:fresh
Rollback migration laravel key point:
- Rollback can undo migrations for the last batch, multiple batches, or all migrations.
- migrate:rollback for the last batch.
- migrate:reset to rollback all migrations.
- migrate:refresh to rollback and re-run migrations.
- migrate:fresh to drop all tables and re-run migrations from scratch.
I hope this tutorial help you.