Laravel Migrations Reference

A Laravel migrations reference: creating tables, modifying and dropping columns, indexes, foreign keys, and running or rolling back migrations.

What a migration is

A migration is a version-controlled change to your database schema. Each file has an up() method that applies the change and a down() method that reverses it. Laravel records which files have run in the migrations table, so php artisan migrate only runs the new ones. For the column types and modifiers used below, see the database field types guide.

Creating a migration

php artisan make:migration create_posts_table
php artisan make:migration add_slug_to_posts_table
php artisan make:migration create_posts_table --create=posts
php artisan make:migration add_slug_to_posts_table --table=posts

# Model + migration (+ factory, seeder, controller) in one go
php artisan make:model Post -m
php artisan make:model Post -mfsc

Files are prefixed with a timestamp, which sets the order they run in. The name matters: create_*_table pre-fills a Schema::create stub and add_*_to_*_table / remove_*_from_*_table pre-fill Schema::table, so --create and --table are usually unnecessary. See the Artisan commands guide for other generators.

Creating a table

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('title');
            $table->string('slug')->unique();
            $table->text('body')->nullable();
            $table->boolean('published')->default(false);
            $table->timestamps();
            $table->softDeletes();

            $table->index(['user_id', 'published']);
        });
    }

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

Always make down() the exact opposite of up() so a rollback leaves the schema as it was.

Modifying an existing table

Never edit a migration that has already run in a shared or production database. Add a new migration that uses Schema::table instead.

public function up(): void
{
    Schema::table('subscription_domains', function (Blueprint $table) {
        $table->text('notes')->nullable()->after('domain');
        $table->boolean('uses_forwarding')->default(false);
    });
}

public function down(): void
{
    Schema::table('subscription_domains', function (Blueprint $table) {
        $table->dropColumn(['notes', 'uses_forwarding']);
    });
}

Changing, renaming and dropping columns

Schema::table('posts', function (Blueprint $table) {
    // Change a column: restate its FULL definition, as unspecified
    // modifiers (nullable, default, ...) are dropped on Laravel 11+
    $table->string('title', 150)->nullable()->change();

    $table->renameColumn('body', 'content');

    $table->dropColumn('slug');
    $table->dropColumn(['published', 'deleted_at']);
    $table->dropSoftDeletes();
    $table->dropTimestamps();
});

Schema::rename('posts', 'articles');

Indexes and foreign keys

AddDrop
$table->index('title')$table->dropIndex(['title'])
$table->unique('email')$table->dropUnique(['email'])
$table->primary('id')$table->dropPrimary()
$table->foreignId('user_id')->constrained()$table->dropForeign(['user_id'])
$table->foreignId('user_id')->constrained() (then drop the column too)$table->dropConstrainedForeignId('user_id')

Passing an array to the drop methods lets Laravel work out the conventional index name (posts_title_index, posts_user_id_foreign). Drop a foreign key before dropping its column on databases that enforce it.

Dropping a table

Schema::drop('posts');
Schema::dropIfExists('posts');   // safer: no error if it is already gone

Dropping data cannot be undone by down(). If you remove a feature, it is common to leave down() as a documented no-op rather than recreate the tables.

Checking the schema first

if (! Schema::hasTable('posts')) { /* ... */ }
if (! Schema::hasColumn('posts', 'slug')) { /* ... */ }
Schema::hasIndex('posts', ['slug']);

Handy for migrations that must be safe to re-run, or when a column may already have been added by hand.

Running and rolling back

CommandWhat it does
php artisan migrateRuns every pending migration.
php artisan migrate --pretendPrints the SQL that would run without executing it.
php artisan migrate --forceRuns without the confirmation prompt in production (use in deploy scripts).
php artisan migrate:statusLists each migration and whether it has run.
php artisan migrate:rollbackReverses the last batch. Add --step=1 to reverse a set number of migrations.
php artisan migrate:resetRolls back every migration.
php artisan migrate:refreshRolls everything back, then migrates again.
php artisan migrate:freshDrops all tables (ignoring down()) then migrates. Add --seed to seed afterwards.

migrate:fresh, refresh and reset destroy data. Use them on a local database only.

Common gotchas

  • Foreign key type mismatch. The referencing column must match the referenced one exactly. Use foreignId() against an id() column.
  • Adding a NOT NULL column to a table with rows. Give it a default() or make it nullable(), otherwise the migration fails.
  • Migration order. A table must exist before another references it, so the timestamp prefix has to sort the parent migration first.
  • Large tables. Adding an index or changing a column can lock the table for the duration. Plan these for quiet periods.
  • SQLite in tests. Some operations (such as dropping a column with a foreign key) behave differently on SQLite, which matters if your test suite uses it with RefreshDatabase.