In Laravel, both php artisan migrate and php artisan migrate:fresh are used for handling database migrations, but they serve different purposes and behave differently. Let’s break it down clearly:
1. php artisan migrate
- Purpose: Applies new migrations that haven’t been run yet.
- Behavior:
- Looks at the
migrationstable in the database. - Runs only the migrations that are not yet executed.
- Does not delete existing tables or data.
- Looks at the
- Use case: When you add a new migration (like creating a new table or modifying an existing one) and want to apply it without losing existing data.
Example:
php artisan migrate
- If you already ran
userstable migration before, Laravel will skip it and only run migrations that are new.
2. php artisan migrate:fresh
- Purpose: Drops all tables in the database and then runs all migrations from scratch.
- Behavior:
- Deletes all existing tables in the database.
- Runs all migrations from the beginning.
- Any existing data will be lost.
- Use case: Useful during development when you want to reset the database completely and start fresh.
Often used together with seeders:
php artisan migrate:fresh --seed
This will reset your database and populate it with seed data.
Summary Table
| Command | Deletes Existing Tables? | Runs Only New Migrations? | Data Loss | Use Case |
|---|---|---|---|---|
php artisan migrate | ❌ No | ✅ Yes | ❌ No | Apply new migrations safely |
php artisan migrate:fresh | ✅ Yes | ❌ No (runs all) | ✅ Yes | Reset DB in development |
💡 Tip: Never use migrate:fresh in production as it wipes all data. It’s meant only for local/dev environments.
