Migrations are version control for your database schema — plain PHP files describing how to build (and unbuild) tables, so your whole team and every environment stay in sync without ever running raw SQL by hand.
php artisan make:migration create_posts_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::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->text('body');
$table->boolean('published')->default(false);
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('posts');
}
};
Run pending migrations with php artisan migrate. Useful variants: migrate:rollback (undo the last batch), migrate:refresh (roll back everything and re-run), and migrate:fresh (drop all tables and re-run from scratch — great for local development, never on production). php artisan make:migration add_status_to_posts_table --table=posts scaffolds an alter-table migration for adding columns later.