Generates a migration file with the create table scaffold:
use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;return new class extends Migration{ /** * Run the migrations. */ public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('posts'); }};
Generates a migration to modify an existing 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::table('posts', function (Blueprint $table) { // }); } public function down(): void { Schema::table('posts', function (Blueprint $table) { // }); }};
Add your column definitions:
public function up(): void{ Schema::table('posts', function (Blueprint $table) { $table->timestamp('published_at')->nullable()->after('created_at'); });}public function down(): void{ Schema::table('posts', function (Blueprint $table) { $table->dropColumn('published_at'); });}
Creates a blank migration for custom modifications:
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::table('posts', function (Blueprint $table) { $table->index('published_at'); $table->index(['author_id', 'published_at']); }); } public function down(): void { Schema::table('posts', function (Blueprint $table) { $table->dropIndex(['author_id', 'published_at']); $table->dropIndex(['published_at']); }); }};
# Rollback the last batchphp artisan migrate:rollback# Rollback all migrationsphp artisan migrate:reset# Rollback and re-run all migrationsphp artisan migrate:refresh
public function up(): void{ Schema::table('posts', function (Blueprint $table) { $table->enum('status', ['draft', 'published', 'archived']) ->default('draft'); });}
public function up(): void{ Schema::table('posts', function (Blueprint $table) { $table->softDeletes(); });}public function down(): void{ Schema::table('posts', function (Blueprint $table) { $table->dropSoftDeletes(); });}
Always implement the down() method to reverse the up() method:
public function up(): void{ Schema::table('posts', function (Blueprint $table) { $table->string('slug')->unique(); });}public function down(): void{ Schema::table('posts', function (Blueprint $table) { $table->dropColumn('slug'); });}
Use descriptive names
Migration names should clearly describe what they do:
✅ add_published_at_to_posts_table
✅ create_post_tag_pivot_table
❌ update_posts
❌ fix_database
One change per migration
Keep migrations focused on a single change for easier rollback: