LaravelPHPDatabaseMigrationTips
Laravel migrations are a powerful way to version-control your database. Here are some examples:
1️⃣ Create Table
php artisan make:migration create_posts_table --create=posts
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
2️⃣ Add New Field
php artisan make:migration add_excerpt_to_posts_table --table=posts
Schema::table('posts', function (Blueprint $table) {
$table->string('excerpt', 500)->after('title');
});
3️⃣ Rename Field
php artisan make:migration rename_excerpt_to_summary_in_posts_table --table=posts
Schema::table('posts', function (Blueprint $table) {
$table->renameColumn('excerpt', 'summary');
});
4️⃣ Change Data Type
php artisan make:migration change_summary_type_in_posts_table --table=posts
Schema::table('posts', function (Blueprint $table) {
$table->text('summary')->change();
});
5️⃣ Add Enum Field
php artisan make:migration add_status_to_posts_table --table=posts
Schema::table('posts', function (Blueprint $table) {
$table->enum('status', ['draft', 'published', 'archived'])->default('draft');
});
6️⃣ Drop Field
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn('summary');
});
✅ With migrations, every database change is tracked and reversible with php artisan migrate:rollback. This keeps your schema consistent across all environments.
About the Author
HK
Harsh Kadiya
Senior iOS & Flutter Developer