Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Running & Rolling Back Migrations
Laravel tracks which migrations have been applied in the migrations table. You can migrate, rollback, reset, and refresh your database from the command line. Understanding these commands is essential for development and deployment workflows.
Migration Commands
php artisan migrate # Run all pending migrations
php artisan migrate --seed # Migrate and seed in one command
php artisan migrate:status # Show which migrations have run
php artisan migrate:rollback # Undo the last batch
php artisan migrate:rollback --step=3 # Undo last 3 migrations
php artisan migrate:reset # Rollback ALL migrations
php artisan migrate:refresh # Reset + re-migrate
php artisan migrate:fresh # Drop ALL tables + re-migrate
php artisan migrate:fresh --seed # Drop + migrate + seed (development)Migration Best Practices
- Never edit a migration that's already been run in production - create a new migration to modify the table
- Always write the down() method so rollbacks work correctly
- Use migrate:fresh --seed in development for a clean slate. Never use migrate:fresh in production
- Test migrations on a copy of production data before deploying to catch data loss issues
- Keep migrations small and focused - one table change per migration for easy rollbacks
Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Tip
Tip
Practice Running Rolling Back Migrations in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Middleware = filters that run before/after your controller logic
Practice Task
Note
Practice Task - (1) Write a working example of Running Rolling Back Migrations from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Running Rolling Back Migrations is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.
Key Takeaways
- Laravel tracks which migrations have been applied in the migrations table.
- Never edit a migration that's already been run in production - create a new migration to modify the table
- Always write the down() method so rollbacks work correctly
- Use migrate:fresh --seed in development for a clean slate. Never use migrate:fresh in production