Overview
The Migration system provides version control for your database schema, allowing you to easily modify and share database structure across environments. It consists of two classes:Migration (base class for migrations) and Migrator (manages migration execution).
Class Reference
Lyger\Database\Migration
Base class for all database migrations. Location:Lyger/Database/Migration.php
Lyger\Database\Migrator
Manages database migration execution. Location:Lyger/Database/Migration.php:43
Migration Class
Abstract Methods
up()
down()
Helper Methods
getConnection()
getSchema()
Migrator Class
Constructor
string
Path to migrations directory. Defaults to
database/migrations/Methods
path()
string
required
Path to migrations directory
$this for method chaining
Example:
run()
rollback()
reset()
status()
make()
string
required
Name of the migration (e.g., “create_users_table”)
getMigrationsPath()
getMigrations()
loadMigrations()
Creating Migrations
Using make()
The easiest way to create a migration:2026_03_08_120000_CreateUsersTable.php:
Manual Migration Creation
Create a file indatabase/migrations/ with the format: YYYY_MM_DD_HHiiss_DescriptiveName.php
Migration Examples
Creating a Table
Adding Columns to Existing Table
Creating Related Tables
Creating Pivot Tables
Using Raw SQL
Complex Migration
Running Migrations
Run All Pending Migrations
Check Migration Status
Rollback Last Batch
Reset All Migrations
Fresh Migration (Reset + Run)
Migration Batching
Migrations are run in batches. Each time you callrun(), all pending migrations are executed as a single batch. When you rollback(), the last batch is reversed.
Example workflow:
Migrations Table
The Migrator automatically creates amigrations table to track which migrations have been run:
This table is created automatically the first time you run migrations. You don’t need to create it manually.
Best Practices
- Never modify existing migrations that have been run in production. Create a new migration instead.
- Always provide a
down()method to reverse your migration - Test migrations locally before deploying to production
- Use descriptive names for migrations (e.g.,
create_users_table, not justusers) - Keep migrations focused - one logical change per migration
- Order matters - migrations run in alphabetical order by filename
- Use the Schema builder instead of raw SQL when possible for database portability
Common Patterns
Renaming a Table
Seeding Data in Migration
Conditional Migrations
Related
- Model - Eloquent-style ORM models
- QueryBuilder - Fluent query builder
- Schema - Database schema builder