0
0
LaravelHow-ToBeginner · 3 min read

How to Run Migration in Laravel: Simple Guide

To run migrations in Laravel, use the php artisan migrate command in your terminal. This command applies all pending migrations to your database, creating or modifying tables as defined in your migration files.
📐

Syntax

The basic command to run migrations in Laravel is php artisan migrate. This command looks for migration files in the database/migrations folder and applies any new migrations to your database.

You can also use options like --force to run migrations in production without confirmation, or --step to run migrations one batch at a time.

bash
php artisan migrate
php artisan migrate --force
php artisan migrate --step
💻

Example

This example shows how to create a migration and then run it to create a new table in your database.

bash
php artisan make:migration create_tasks_table --create=tasks

# After editing the migration file to define columns, run:
php artisan migrate
Output
Migrating: 2024_06_01_000000_create_tasks_table Migrated: 2024_06_01_000000_create_tasks_table (0.05 seconds)
⚠️

Common Pitfalls

  • Not setting up your database connection correctly in .env causes migration failures.
  • Running php artisan migrate without creating migration files first does nothing.
  • Forgetting to run composer dump-autoload after creating migrations can cause Laravel not to find new migration classes.
  • Running migrations in production without --force will prompt for confirmation and may halt automated scripts.
bash
Wrong way:
php artisan migrate

Right way:
php artisan migrate --force
📊

Quick Reference

CommandDescription
php artisan migrateRun all pending migrations
php artisan migrate:rollbackUndo the last batch of migrations
php artisan migrate:resetUndo all migrations
php artisan migrate:refreshRollback all migrations and run them again
php artisan migrate --forceRun migrations in production without confirmation

Key Takeaways

Use php artisan migrate to apply new migrations to your database.
Ensure your database connection is correctly set in the .env file before running migrations.
Use --force to run migrations safely in production environments.
Create migration files first with php artisan make:migration before running migrations.
Run composer dump-autoload if Laravel does not detect new migration files.