Laravel Migrations

Print Print
Reading time 0:52

Migrations in Laravel are version control for your database schema. They allow a team to define and share the application's database structure using PHP code instead of manual SQL, making it easy to build, modify, and roll back tables consistently across environments.

Creating a Migration

Generate a migration

php artisan make:migration create_posts_table

This generates a timestamped file in database/migrations containing up and down methods, used to apply and reverse the change respectively.

Defining Columns

Example migration

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('body');
        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('posts');
}

Running Migrations

Common migration commands

php artisan migrate           # Apply pending migrations
php artisan migrate:rollback  # Undo the last batch of migrations
php artisan migrate:fresh     # Drop all tables and re-run migrations

Migration Tracking

Laravel keeps track of which migrations have already run in a migrations table, so running php artisan migrate repeatedly only applies migrations that have not yet been executed.

By: Tomas Silny
Edited: 2026-08-13 03:31:18