laravel-migrations-and-factories
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMigrations and Factories
迁移与工厂类
Keep schema changes safe, testable, and reversible.
确保数据库架构变更安全、可测试且可回滚。
Commands
命令
php artisan make:model Post -mfcphp artisan make:model Post -mfcRun/rollback
执行/回滚
php artisan migrate
php artisan migrate:rollback --step=1
php artisan migrate
php artisan migrate:rollback --step=1
Fresh DB (dangerous; dev only)
重置数据库(危险;仅用于开发环境)
php artisan migrate:fresh --seed
undefinedphp artisan migrate:fresh --seed
undefinedRules
规则
- Pair each new model with a migration and a factory
- If a migration was merged to , never edit it—add a new one
main - 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
- 每个新模型都要配对对应的迁移和工厂类
- 若迁移已合并到分支,绝不要编辑它——新增一个新的迁移
main - 在功能分支中,你可以修改该分支上创建的迁移(如果尚未合并)
- 在填充器类中填充真实但精简的数据集;大型数据集请存放在外部
Factories
工厂类
- Prefer state modifiers (e.g., ) over boolean flags
->state([...]) - Use relationships (e.g., ) in factories to build realistic graphs
belongsTo - Keep factories fast; move expensive setup to seeds where possible
- 优先使用状态修饰符(如)而非布尔标记
->state([...]) - 在工厂类中使用关联关系(如)来构建真实的数据关联图
belongsTo - 保持工厂类执行快速;尽可能将耗时的设置移到填充器中
Testing
测试
- Use factories in tests; avoid manual inserts
- For integration tests touching DB, use transactions or
RefreshDatabase
- 在测试中使用工厂类;避免手动插入数据
- 对于涉及数据库的集成测试,使用事务或特性
RefreshDatabase