DEV Community

Cover image for Laravel migrations
Anas Hussain
Anas Hussain

Posted on

Laravel migrations

1. Migration Basics

Create a Migration

php artisan make:migration create_table_name_table
Enter fullscreen mode Exit fullscreen mode

Example:

php artisan make:migration create_users_table
Enter fullscreen mode Exit fullscreen mode

Run Migrations

Run all pending migrations:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Rollback Migrations

Rollback the most recent batch of migrations:

php artisan migrate:rollback
Enter fullscreen mode Exit fullscreen mode

Rollback & Re-run Migrations

Rollback and re-run all migrations:

php artisan migrate:refresh
Enter fullscreen mode Exit fullscreen mode

Reset Migrations

Rollback all migrations completely:

php artisan migrate:reset
Enter fullscreen mode Exit fullscreen mode

Run Specific Migration

Run a single migration file:

php artisan migrate --path=/database/migrations/filename.php
Enter fullscreen mode Exit fullscreen mode

Status of Migrations

Check migration status (applied or pending):

php artisan migrate:status
Enter fullscreen mode Exit fullscreen mode

2. Table Operations

Create a Table

To create a table, use:

Schema::create('table_name', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamps();
});
Enter fullscreen mode Exit fullscreen mode

Modify an Existing Table

To modify an existing table, you need to create a new migration:

php artisan make:migration add_columns_to_table_name_table
Enter fullscreen mode Exit fullscreen mode

3. Adding Columns

Add a New Column

Schema::table('table_name', function (Blueprint $table) {
    $table->string('new_column');
});
Enter fullscreen mode Exit fullscreen mode

Add Multiple Columns

Schema::table('table_name', function (Blueprint $table) {
    $table->integer('age');
    $table->string('address')->nullable();
});
Enter fullscreen mode Exit fullscreen mode

Add Column with Default Value

Schema::table('table_name', function (Blueprint $table) {
    $table->boolean('status')->default(1);
});
Enter fullscreen mode Exit fullscreen mode

4. Dropping Columns

Drop a Single Column

Schema::table('table_name', function (Blueprint $table) {
    $table->dropColumn('column_name');
});
Enter fullscreen mode Exit fullscreen mode

Drop Multiple Columns

Schema::table('table_name', function (Blueprint $table) {
    $table->dropColumn(['column1', 'column2']);
});
Enter fullscreen mode Exit fullscreen mode

5. Rename Operations

Rename a Column

Schema::table('table_name', function (Blueprint $table) {
    $table->renameColumn('old_name', 'new_name');
});
Enter fullscreen mode Exit fullscreen mode

Rename a Table

Schema::rename('old_table_name', 'new_table_name');
Enter fullscreen mode Exit fullscreen mode

6. Altering Columns

Change Column Type

Install the doctrine/dbal package:

composer require doctrine/dbal
Enter fullscreen mode Exit fullscreen mode

Then use:

Schema::table('table_name', function (Blueprint $table) {
    $table->string('column_name')->change();
});
Enter fullscreen mode Exit fullscreen mode

Make Column Nullable

Schema::table('table_name', function (Blueprint $table) {
    $table->string('column_name')->nullable()->change();
});
Enter fullscreen mode Exit fullscreen mode

7. Indexing

Add an Index

$table->index('column_name');
Enter fullscreen mode Exit fullscreen mode

Add a Unique Index

$table->unique('column_name');
Enter fullscreen mode Exit fullscreen mode

Add a Composite Index

$table->index(['column1', 'column2']);
Enter fullscreen mode Exit fullscreen mode

Drop an Index

$table->dropIndex('index_name');
Enter fullscreen mode Exit fullscreen mode

Drop a Unique Index

$table->dropUnique('table_name_column_name_unique');
Enter fullscreen mode Exit fullscreen mode

8. Foreign Keys

Add Foreign Key

Schema::table('table_name', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Enter fullscreen mode Exit fullscreen mode

Drop Foreign Key

$table->dropForeign('table_name_user_id_foreign');
Enter fullscreen mode Exit fullscreen mode

9. Timestamps and Soft Deletes

Add Timestamps

$table->timestamps();
Enter fullscreen mode Exit fullscreen mode

Add Soft Deletes

$table->softDeletes();
Enter fullscreen mode Exit fullscreen mode

Drop Soft Deletes

$table->dropSoftDeletes();
Enter fullscreen mode Exit fullscreen mode

10. Miscellaneous

Add Remember Token

$table->rememberToken();
Enter fullscreen mode Exit fullscreen mode

Add UUID as Primary Key

$table->uuid('id')->primary();
Enter fullscreen mode Exit fullscreen mode

Check Table Existence

if (!Schema::hasTable('table_name')) {
    Schema::create('table_name', function (Blueprint $table) {
        $table->id();
    });
}
Enter fullscreen mode Exit fullscreen mode

Check Column Existence

if (!Schema::hasColumn('table_name', 'column_name')) {
    Schema::table('table_name', function (Blueprint $table) {
        $table->string('column_name');
    });
}
Enter fullscreen mode Exit fullscreen mode

11. Migration Rollbacks with Specific Steps

Rollback Last 'N' Migrations

Rollback a specific number of migrations:

php artisan migrate:rollback --step=2
Enter fullscreen mode Exit fullscreen mode

Rollback and Re-run Specific Steps

php artisan migrate:refresh --step=2
Enter fullscreen mode Exit fullscreen mode

12. Running Migrations in Production

To force migration in production without a prompt:

php artisan migrate --force
Enter fullscreen mode Exit fullscreen mode

Practice Tips

  1. Practice table creation with all column types (string, integer, boolean, json, text, etc.).
  2. Simulate database changes using rollback, refresh, and reset.
  3. Implement foreign key constraints with cascading deletes.
  4. Work on adding soft deletes and testing data recovery.
  5. Try renaming columns and tables using migration.

Top comments (0)