Skip to main content

Overview

The Schema class provides a fluent interface for creating and modifying database tables. It works with a Blueprint class to define table structures with methods inspired by Laravel’s schema builder.

Class Reference

Lyger\Database\Schema

Schema builder for database table operations. Location: Lyger/Database/Schema.php

Schema Methods

Constructor

Create a new Schema instance.
string
Database driver (‘sqlite’, ‘mysql’, ‘pgsql’). Auto-detected if not provided.
Example:

create()

Create a new database table.
string
required
Name of the table to create
callable
required
Closure that receives a Blueprint instance
Example:

table()

Modify an existing database table.
string
required
Name of the table to modify
callable
required
Closure that receives a Blueprint instance
Example:

drop()

Drop a database table.
string
required
Name of the table to drop
Example:

dropIfExists()

Drop a table if it exists.
string
required
Name of the table to drop
Example:

Blueprint Class

Lyger\Database\Blueprint

Fluent interface for defining table structure. Location: Lyger/Database/Schema.php:186

Column Types

id()

Create an auto-incrementing INTEGER primary key column.
string
default:"'id'"
Column name
Example:

bigId()

Create an auto-incrementing big INTEGER primary key column.
string
default:"'id'"
Column name
Example:

string()

Create a VARCHAR column.
string
required
Column name
int
default:"255"
Maximum string length
Example:

text()

Create a TEXT column.
string
required
Column name
Example:

integer()

Create an INTEGER column.
string
required
Column name
bool
default:"false"
Whether the integer is unsigned
Example:

bigInteger()

Create a big INTEGER column.
string
required
Column name
bool
default:"false"
Whether the integer is unsigned
Example:

decimal()

Create a DECIMAL column (stored as REAL in SQLite).
string
required
Column name
int
default:"8"
Total number of digits
int
default:"2"
Number of decimal places
Example:

float()

Create a FLOAT column (stored as REAL in SQLite).
string
required
Column name
Example:

boolean()

Create a BOOLEAN column (stored as INTEGER in SQLite).
string
required
Column name
Example:

date()

Create a DATE column.
string
required
Column name
Example:

datetime()

Create a DATETIME column.
string
required
Column name
Example:

timestamp()

Create a TIMESTAMP column.
string
required
Column name
Example:

timestamps()

Add created_at and updated_at TIMESTAMP columns. Example:

softDeletes()

Add a deleted_at TIMESTAMP column for soft deletes. Example:

json()

Create a JSON column (stored as TEXT in SQLite).
string
required
Column name
Example:

uuid()

Create a UUID column (36 character string).
string
default:"'uuid'"
Column name
Example:

Column Modifiers

These methods modify the most recently defined column.

nullable()

Mark the column as nullable. Example:

default()

Set a default value for the column.
mixed
required
Default value
Example:

unsigned()

Mark the integer column as unsigned. Example:

primary()

Mark the column as a primary key. Example:

unique()

Add a unique index to the column. Example:

index()

Add an index to the column(s).
array
default:"[]"
Array of column names. If empty, indexes the last defined column.
Example:

Usage Examples

Creating a Users Table

Creating a Posts Table with Foreign Key

Creating a Pivot Table

Adding Columns to Existing Table

Complex Table with All Column Types

E-commerce Example

Dropping Tables

Column Type Reference

Best Practices

  • Use id() for most tables to create a standard auto-incrementing primary key
  • Always add timestamps() to track record creation and updates
  • Use unique() on columns that must have distinct values (emails, usernames, etc.)
  • Add index() to columns frequently used in WHERE clauses and JOINs
  • Use nullable() for optional fields
  • Use default() to set sensible defaults for columns
  • Use descriptive column names in snake_case

Database Drivers

The Schema builder supports multiple database drivers: