1. Migration Basics
Create a Migration
php artisan make:migration create_table_name_table
Example:
php artisan make:migration create_users_table
Run Migrations
Run all pending migrations:
php artisan migrate
Rollback Migrations
Rollback the most recent batch of migrations:
php artisan migrate:rollback
Rollback & Re-run Migrations
Rollback and re-run all migrations:
php artisan migrate:refresh
Reset Migrations
Rollback all migrations completely:
php artisan migrate:reset
Run Specific Migration
Run a single migration file:
php artisan migrate --path=/database/migrations/filename.php
Status of Migrations
Check migration status (applied or pending):
php artisan migrate:status
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();
});
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
3. Adding Columns
Add a New Column
Schema::table('table_name', function (Blueprint $table) {
$table->string('new_column');
});
Add Multiple Columns
Schema::table('table_name', function (Blueprint $table) {
$table->integer('age');
$table->string('address')->nullable();
});
Add Column with Default Value
Schema::table('table_name', function (Blueprint $table) {
$table->boolean('status')->default(1);
});
4. Dropping Columns
Drop a Single Column
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column_name');
});
Drop Multiple Columns
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn(['column1', 'column2']);
});
5. Rename Operations
Rename a Column
Schema::table('table_name', function (Blueprint $table) {
$table->renameColumn('old_name', 'new_name');
});
Rename a Table
Schema::rename('old_table_name', 'new_table_name');
6. Altering Columns
Change Column Type
Install the doctrine/dbal package:
composer require doctrine/dbal
Then use:
Schema::table('table_name', function (Blueprint $table) {
$table->string('column_name')->change();
});
Make Column Nullable
Schema::table('table_name', function (Blueprint $table) {
$table->string('column_name')->nullable()->change();
});
7. Indexing
Add an Index
$table->index('column_name');
Add a Unique Index
$table->unique('column_name');
Add a Composite Index
$table->index(['column1', 'column2']);
Drop an Index
$table->dropIndex('index_name');
Drop a Unique Index
$table->dropUnique('table_name_column_name_unique');
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');
});
Drop Foreign Key
$table->dropForeign('table_name_user_id_foreign');
9. Timestamps and Soft Deletes
Add Timestamps
$table->timestamps();
Add Soft Deletes
$table->softDeletes();
Drop Soft Deletes
$table->dropSoftDeletes();
10. Miscellaneous
Add Remember Token
$table->rememberToken();
Add UUID as Primary Key
$table->uuid('id')->primary();
Check Table Existence
if (!Schema::hasTable('table_name')) {
Schema::create('table_name', function (Blueprint $table) {
$table->id();
});
}
Check Column Existence
if (!Schema::hasColumn('table_name', 'column_name')) {
Schema::table('table_name', function (Blueprint $table) {
$table->string('column_name');
});
}
11. Migration Rollbacks with Specific Steps
Rollback Last 'N' Migrations
Rollback a specific number of migrations:
php artisan migrate:rollback --step=2
Rollback and Re-run Specific Steps
php artisan migrate:refresh --step=2
12. Running Migrations in Production
To force migration in production without a prompt:
php artisan migrate --force
Practice Tips
- Practice table creation with all column types (
string
,integer
,boolean
,json
,text
, etc.). - Simulate database changes using rollback, refresh, and reset.
- Implement foreign key constraints with cascading deletes.
- Work on adding soft deletes and testing data recovery.
- Try renaming columns and tables using migration.
Top comments (0)