🔹 1. What are Laravel Migrations?
Laravel migrations are version-controlled database schema definitions. They let you:
- Create, update, and rollback tables using PHP code.
- Work with teams and keep DB changes consistent.
- Use CLI (
artisan
) instead of manual SQL.
🔹 2. How to Create a Migration
It creates a file like:
✅ Use
php artisan migrate
to apply the migration.🔹 3. Common Column Types
🔹 4. Adding Columns to Existing Table
In the
up
method, you define the changes you want to apply to your database. To add a new column to an existing table, use the Schema::table()
method.To place the new column in a specific position, use the after() method:
The ‘down’ method is used to reverse the changes made in the ‘up’ method. This is important for rollback functionality.
🔹 5. Modifying Columns
🔹 6. Dropping Columns
🔹 7. Renaming Columns
🔹 8. Adding Indexes
🔹 9. Rolling Back
Rolls back last batch
To rollback multiple steps, use the — step option
The
php artisan migrate:reset
command is used to roll back all of your database migrations in reverse order, undoing everything that has been migrated — without dropping the tables manually like migrate:fresh
.🔄
php artisan migrate:refresh
Explained- Rolls back all existing migrations (like
migrate:reset
) - Re-runs all migrations from the beginning (like
migrate
)
🔄
php artisan migrate:fresh
Explained- Dropping all existing tables.
- Re-running all of your migration files from scratch.
🔹 10. Best Practices
✅ Keep migration files small & focused
✅ Add default values for non-nullable columns
✅ Use
✅ Always provide a
✅ Use
✅ Add default values for non-nullable columns
✅ Use
nullable()
for optional columns✅ Always provide a
down()
method for rollback✅ Use
php artisan migrate --pretend
to see SQL ✅ Summary
Laravel migrations are your version control for databases. Once you master them, you’ll be able to:
- Avoid SQL mistakes
- Collaborate with ease
- Keep your DB schema clean and trackable
If you found this helpful, feel free to share or drop a comment. Happy coding with Laravel! 🧱✨
Social Plugin