Advertisement
Google Ad Slot: content-top
Laravel Migration
What are Migrations?
- Migrations in Laravel are version control for your database schema.
- They allow you to create, modify, and delete tables & columns using PHP code instead of SQL queries.
- They make database changes trackable, team-friendly, and reversible.
Why Use Migrations?
- Database schema version control.
- Share schema changes across teams.
- Rollback changes if something breaks.
- No need to write raw SQL.
Update .env
Update .env file variable for Database connection:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=my_app DB_USERNAME=root DB_PASSWORD=
Migration Types (Like Route Types)
Just like routes have multiple types, Migrations also have different operations.
- Create Table Migration
- Modify Table Migration
Migration Commands
Command |
Description |
|---|---|
php artisan make:migration create_users_table |
Create a new migration file |
php artisan migrate |
Run all pending migrations |
php artisan migrate:rollback |
Rollback the last batch of migrations |
php artisan migrate:reset |
Rollback all migrations |
php artisan migrate:refresh |
Rollback & re-run all migrations |
php artisan migrate:fresh |
Drop all tables & re-run migrations |
php artisan migrate:status |
Show migration status (which are run / pending) |