Overview
TheSchema 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
string
Database driver (‘sqlite’, ‘mysql’, ‘pgsql’). Auto-detected if not provided.
create()
string
required
Name of the table to create
callable
required
Closure that receives a Blueprint instance
table()
string
required
Name of the table to modify
callable
required
Closure that receives a Blueprint instance
drop()
string
required
Name of the table to drop
dropIfExists()
string
required
Name of the table to drop
Blueprint Class
Lyger\Database\Blueprint
Fluent interface for defining table structure. Location:Lyger/Database/Schema.php:186
Column Types
id()
string
default:"'id'"
Column name
bigId()
string
default:"'id'"
Column name
string()
string
required
Column name
int
default:"255"
Maximum string length
text()
string
required
Column name
integer()
string
required
Column name
bool
default:"false"
Whether the integer is unsigned
bigInteger()
string
required
Column name
bool
default:"false"
Whether the integer is unsigned
decimal()
string
required
Column name
int
default:"8"
Total number of digits
int
default:"2"
Number of decimal places
float()
string
required
Column name
boolean()
string
required
Column name
date()
string
required
Column name
datetime()
string
required
Column name
timestamp()
string
required
Column name
timestamps()
created_at and updated_at TIMESTAMP columns.
Example:
softDeletes()
deleted_at TIMESTAMP column for soft deletes.
Example:
json()
string
required
Column name
uuid()
string
default:"'uuid'"
Column name
Column Modifiers
These methods modify the most recently defined column.nullable()
default()
mixed
required
Default value
unsigned()
primary()
unique()
index()
array
default:"[]"
Array of column names. If empty, indexes the last defined column.
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:Related
- Model - Eloquent-style ORM models
- QueryBuilder - Fluent query builder
- Migration - Database migration system