Migrations are version control for your database schema.
Commands
php artisan make:migration create_posts_tablephp artisan migratephp artisan migrate:rollback
Migrations are version control for your database schema.
php artisan make:migration create_posts_tablephp artisan migratephp artisan migrate:rollbackSchema::create("posts", function (Blueprint $table) { $table->id(); $table->foreignId("user_id")->constrained(); $table->string("title"); $table->text("body"); $table->boolean("published")->default(false); $table->timestamps(); });
<?php 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->text("body"); $table->boolean("is_published")->default(false); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists("posts"); } }; ?>
Create a migration for a comments table.
Schema::create("comments", function ($table) {
$table->id();
$table->foreignId("user_id")->constrained();
$table->foreignId("post_id")->constrained();
$table->text("body");
$table->timestamps();
});
What does migrate:rollback do?
Rollback reverses the last batch of migrations.
They track schema changes in version control, can be rolled back, work across environments.