Introduction
Migrations are version control for your database schema. They allow you to define and share your application’s database schema definition, making it easy to modify and share the database structure across your team.Creating Migrations
Using the Migrator
Create a new migration file using theMigrator class:
database/migrations/ directory with a timestamp prefix.
Migration File Structure
A generated migration file looks like this:database/migrations/2026_03_08_143052_create_users_table.php
Migration Structure
Every migration has two methods:void
Runs when the migration is executed. Define table creation and modifications here.
void
Runs when the migration is rolled back. Reverse the changes made in
up().Creating Tables
Basic Table Creation
Complete Example
Available Column Types
Numeric Types
- Integers
- Decimals
- Boolean
String Types
Date and Time Types
Special Types
Column Modifiers
Chain modifiers to customize column behavior:Nullable Columns
Default Values
Unsigned Integers
Indexes
Modifying Tables
Adding Columns
Dropping Tables
Drop Table
Drop If Exists
Running Migrations
Run All Pending Migrations
Custom Migration Path
Rolling Back Migrations
Rollback Last Batch
Reset All Migrations
Migration Status
Check which migrations have been run:Complete Migration Examples
Users Table
Posts Table with Foreign Key
Pivot Table for Many-to-Many
Best Practices
1
Always Include Timestamps
Use
$table->timestamps() to track creation and modification times:2
Use Descriptive Names
Name your migrations clearly:
create_users_tableadd_status_to_posts_tablecreate_post_tag_pivot_table
3
Make Migrations Reversible
Always implement the
down() method to reverse changes:4
Use Soft Deletes
For data that might need recovery, use soft deletes:
5
Add Indexes for Performance
Index columns used in WHERE clauses and JOINs:
Migration Workflow
1
Create Migration
2
Define Schema
Edit the generated file to define your table structure
3
Run Migration
4
Test Your Changes
Verify the table was created correctly
5
Commit to Version Control
Commit the migration file to your repository
Database Drivers
Lyger migrations work with multiple database drivers:- SQLite
- PostgreSQL
- MySQL
Default driver, no additional configuration needed:
.env
The Schema builder automatically adapts to your configured database driver, handling differences in SQL syntax and data types.
Next Steps
Eloquent Models
Learn how to interact with your migrated tables
Relationships
Define relationships between tables