Schema

Schema compiles a dialect-agnostic table description into real CREATE/ALTER TABLE SQL for whichever dialect DB is connected to — MySQL, PostgreSQL, or SQLite.

Tables

Schema::createTable(string $table, Closure $callback, ?string $context = null): void
Schema::alterTable(string $table, Closure $callback, ?string $context = null): void // adds columns only
Schema::dropColumn(string $table, string $column, ?string $context = null): void
Schema::dropTable(string $table, bool $ifExists = true, ?string $context = null): void
Schema::hasTable(string $table, ?string $context = null): bool
Schema::hasColumn(string $table, string $column, ?string $context = null): bool
Schema::createTable('products', function (Blueprint $table) {
    $table->id();
    $table->string('name', 100);
    $table->integer('price');
    $table->boolean('active', default: true);
});

Blueprint column types

$table->id(string $name = 'id')                 // UUID primary key
$table->autoIncrementId(string $name = 'id')    // integer primary key, auto-increment
$table->uuid(string $name, nullable: false, default: null)
$table->string(string $name, int $length = 255, nullable: false, default: null)
$table->text(string $name, nullable: false)
$table->integer(string $name, nullable: false, default: null)
$table->bigInteger(string $name, nullable: false, default: null)
$table->boolean(string $name, nullable: false, default: null)
$table->json(string $name, nullable: false)
$table->datetime(string $name, nullable: false, default: null, autoUpdate: false) // autoUpdate is MySQL-only
$table->binary(string $name, ?int $length = null, nullable: false)

Keys and indexes

$table->primary(string|array $columns)
$table->unique(string|array $columns, ?string $name = null)
$table->index(string|array $columns, ?string $name = null)

Schema::createIndex(string $table, string|array $columns, ?string $name = null, bool $unique = false): void
Schema::dropIndex(string $table, string $name, bool $ifExists = true): void

Raw SQL defaults and dialect

Schema::raw(string $sql) // e.g. Schema::raw('CURRENT_TIMESTAMP') as a column default
Schema::dialect(?string $context = null): string // 'mysql' | 'pgsql' | 'sqlite'

What Schema doesn't paper over

  • datetime(..., autoUpdate: true) only compiles on MySQL — PostgreSQL/SQLite need a trigger, which Schema doesn't generate. Set the column explicitly on those dialects instead.
  • SQLite has no CREATE/DROP DATABASE — a file or :memory: connection already is one.
  • Column modification/renaming isn't supported — cross-dialect ALTER COLUMN semantics differ too much for one abstraction. Drop and recreate the column.

Next steps

  • Migration — running Schema calls from versioned files, in order, with rollback.
  • DB — querying the tables Schema creates.