We use laravel artisan commands to migrate or rollback the database tables. When we use these commands, all tables are migrated or roll backed at once. Sometime, we have to make some changes in single schema and nothing changes in other. As we know, default rollback and migration will have effect on all tables and data will be lost. Our aim is to make changes to single table by preserving data stored in other tables.
We can achieve such specific table rollback and migration with following approach.
What you have to do is just look at the migrations table in your database. There will be a list of migration file name and batch number value.
Suppose you have following structure,
id migration batch 1 2014_10_12_000000_create_users_table 1 2 2014_10_12_100000_create_password_resets_table 1 3 2016_09_07_103432_create_table_roles 1
Suppose we want to make changes in roles table, we have to change it's migration batch value to 2 manually which is highest among all other batch value. Now, we have to run following artisan command.
php artisan migrate:rollback
Here, only table with batch value 2 will be rolled back. Now make changes to that table and run following artisan command.
php artisan migrate
We can use this approach to rollback and migrate more than one table as well so that we can keep data in other tables.
Also Read: How to use database transaction in laravel ?
Batch value in the migrations table defines order of the migrations. when you rollback, migrations that are latest or have highest batch value are rolled back at first and then others. So, you can change the value in database and then rollback a particular migration file.
Although it's not a good idea to change batch number every time because of relationship among the table structure, we can use this case for some cases where single or multiple table rollback doesn't violates the integrity among the tables.