0
0
Laravelframework~3 mins

Why Running and rolling back migrations in Laravel? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could fix database mistakes with a single command instead of hours of manual work?

The Scenario

Imagine you have to create and update your database tables by writing raw SQL commands every time you change your app's data structure.

Then, if something goes wrong, you must manually undo those changes without any easy way to track what was done.

The Problem

Manually running SQL commands is risky and slow.

You can easily forget which changes were applied or make mistakes when trying to undo them.

This leads to inconsistent databases and wasted time fixing errors.

The Solution

Laravel migrations let you write database changes as code that you can run or roll back safely.

This keeps track of every change and lets you move your database forward or backward with simple commands.

Before vs After
Before
CREATE TABLE users (id INT, name VARCHAR(100));
-- To undo: DROP TABLE users;
After
php artisan migrate
php artisan migrate:rollback
What It Enables

You can easily manage database changes across different environments and team members without confusion or errors.

Real Life Example

When adding a new feature that needs a new table, you write a migration and run it on your local machine, then your teammates run the same migration to update their databases automatically.

Key Takeaways

Manual database changes are error-prone and hard to track.

Migrations automate applying and undoing database changes safely.

This keeps your database consistent and your team in sync.