Topics Database & Migrations Migrations & Schema Builder
intermediate 14 min read

Migrations & Schema Builder

Creating migrations, schema Blueprint methods, column modifiers, and squashing migrations.

Migrations

Migrations are version control for your database schema. Generate with php artisan make:migration.

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", 255);
$table->text("body")->nullable();
$table->string("status")->default("draft");
$table->timestamp("published_at")->nullable();
$table->boolean("is_featured")->default(false);
$table->softDeletes();
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists("posts");
}
};

Schema Builder Methods

Schema::create("table", fn (Blueprint $t) => ...);
Schema::table("table", fn (Blueprint $t) => ...);
Schema::rename("from", "to");
Schema::drop("table");
Schema::dropIfExists("table");
Schema::hasTable("table");
Schema::hasColumn("table", "column");

Column Modifiers

$table->string("email")->unique();
$table->string("slug")->index();
$table->unsignedBigInteger("views")->default(0);
$table->string("color", 7)->nullable();
$table->text("bio")->charset("utf8mb4");

// Laravel 13: ULID support
$table->ulid("id")->primary();

// Laravel 13: Vector column type (for AI/embeddings)
$table->vector("embedding", 1536);

Examples

<?php
// Create a migration for a 'categories' table
Schema::create('categories', function (Blueprint \$table) {
    \$table->id();
    \$table->string('name')->unique();
    \$table->string('slug')->unique()->index();
    \$table->text('description')->nullable();
    \$table->boolean('is_active')->default(true);
    \$table->timestamps();
});

Your Notes

Sign in to take notes for this lesson.

Quiz

Database & Migrations Quiz

0 questions

Sign in to take quiz

Discussion

Sign in to join the discussion.

Flashcards

Sign in to create flashcards.