Laravel Migration Commands Cheat Sheet

Laravel Commands

1. Create a new migration:

php artisan make:migration create_users_table

Creates a new migration file in the database/migrations directory.

2. Create a migration for a specific table (if it exists):

php artisan make:migration add_column_to_users_table --table=users

Creates a migration file pre-filled with a schema for the specified table.

3. Create a migration for a model (combines model and migration creation):

php artisan make:model Post -m

Creates both a model and its corresponding migration file.

4. Run all pending migrations:

php artisan migrate

Executes all migrations that haven't been run yet.

5. Run migrations up to a specific version:

php artisan migrate --step=5

# Runs the next 5 migrations

php artisan migrate --version=20231027123456

# Runs migrations up to a specific timestamp

6. Force migrations to run in production (use with caution):

php artisan migrate --force

Forces migrations to run in production (generally discouraged).

7. Run migrations for a specific database connection:

php artisan migrate --database=mysql2

Runs migrations using the specified database connection.

8. Rollback the last migration:

php artisan migrate:rollback

Reverts the changes made by the most recent migration.

9. Rollback a specific number of migrations:

php artisan migrate:rollback --step=3

Rolls back a specified number of migrations.

10. Rollback all migrations:

php artisan migrate:reset

Reverts all migrations (use with extreme caution).

11. Rollback migrations to a specific version:

php artisan migrate:rollback --version=20231026000000

Rolls back migrations to a specific timestamp/version.

12. Rollback migrations for a specific database connection:

php artisan migrate:rollback --database=mysql2

Rolls back migrations using the specified database connection.

13. Refresh the database (rollback all and then migrate):

php artisan migrate:refresh

Rolls back all migrations and then runs them again.

14. Refresh the database with seeders:

php artisan migrate:fresh --seed

Refreshes the database and then runs database seeders.

15. Show the status of migrations:

php artisan migrate:status

Lists all migrations and their status (run or not run).

16. Install the migration table:

php artisan migrate:install

Creates the migrations table (usually not needed manually).