Creating and Running Migrations
Migrations give a Laravel application a reliable, version-controlled way to build and evolve its database schema. Instead of applying changes manually on each environment, migrations describe those changes once, in code that travels with the rest of the project.
Creating a Migration
New migrations are generated through Artisan, producing a timestamped file inside database/migrations. The timestamp ensures migrations always run in the order they were created, which matters when later files depend on tables created earlier.
Structuring a Migration
Each migration file defines two methods: up(), which describes the changes to apply, and down(), which describes how to reverse them. This pairing is what makes migrations safely reversible.
| Command | What It Does |
|---|---|
php artisan make:migration create_posts_table | Creates a new migration file |
php artisan migrate | Runs all pending migrations |
php artisan migrate:rollback | Reverses the most recent batch of migrations |
php artisan migrate:fresh | Drops all tables and re-runs every migration from scratch |
Why Migrations Matter
- Keep the database schema in sync across every developer's machine
- Provide a clear, ordered history of every structural change
- Make deployments predictable, since staging and production run the same files
Creating tables is only the starting point — real applications constantly evolve, which means migrations are just as often used to add columns and modify existing tables.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
