laravel-migrations-and-factories
npx machina-cli add skill noartem/skills/laravel-migrations-and-factories --openclawMigrations and Factories
Keep schema changes safe, testable, and reversible.
Commands
php artisan make:model Post -mfc
# Run/rollback
php artisan migrate
php artisan migrate:rollback --step=1
# Fresh DB (dangerous; dev only)
php artisan migrate:fresh --seed
Rules
- Pair each new model with a migration and a factory
- If a migration was merged to
main, never edit it—add a new one - On feature branches, you may amend migrations created on that branch (if not merged)
- Seed realistic but minimal datasets in seeder classes; keep huge datasets external
Factories
- Prefer state modifiers (e.g.,
->state([...])) over boolean flags - Use relationships (e.g.,
belongsTo) in factories to build realistic graphs - Keep factories fast; move expensive setup to seeds where possible
Testing
- Use factories in tests; avoid manual inserts
- For integration tests touching DB, use transactions or
RefreshDatabase
Source
git clone https://github.com/noartem/skills/blob/main/skills/laravel-migrations-and-factories/SKILL.mdView on GitHub Overview
This skill teaches safe database change patterns in Laravel, emphasizing how and when to modify or add migrations, and the importance of pairing each new model with a migration and a factory. It also covers seeding guidance and practical testing practices to keep schema changes testable and reversible.
How This Skill Works
When you create a new model, scaffold it with a migration and a factory (e.g., php artisan make:model Post -mfc). Do not edit migrations that have already been merged to main; instead create new migrations. On feature branches you may amend migrations created on that branch before merge. Seed data with realistic but minimal datasets, and rely on factories with relationships and state modifiers to generate test data efficiently.
When to Use It
- Creating a new model with a corresponding migration and factory
- Editing an existing migration after it’s been merged to main is prohibited; add a new migration instead
- Amending migrations on a feature branch before merge
- Seeding minimal, realistic data instead of large seed sets
- Using factories in tests and ensuring DB tests run in transactions or with RefreshDatabase
Quick Start
- Step 1: Run php artisan make:model Post -mfc to scaffold model, migration, and factory
- Step 2: Implement your schema in the new migration and define factory data with realistic relations
- Step 3: Use php artisan migrate to apply migrations and use tests with RefreshDatabase; seed minimal data as needed
Best Practices
- Pair each new model with a migration and a factory
- Prefer state modifiers (->state([…])) over boolean flags in factories
- Define relationships (belongsTo, hasMany) in factories to build realistic graphs
- Keep factories fast; move expensive setup to seeds where possible
- Seed realistic but minimal datasets; avoid large datasets in seeders
Example Use Cases
- Add a Post model with a dedicated migration and factory, then seed a small set of posts for development
- On main, create a new migration for a schema change instead of editing an existing one
- On a feature branch, adjust migrations created on that branch before merging
- Use factories in tests with RefreshDatabase to ensure clean DB state
- Run migrate:fresh --seed in dev to reset and seed the database before testing new features