laravel-migrations-and-factories

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Migrations and Factories

迁移与工厂类

Keep schema changes safe, testable, and reversible.
确保数据库架构变更安全、可测试且可回滚。

Commands

命令

php artisan make:model Post -mfc
php artisan make:model Post -mfc

Run/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
undefined
php artisan migrate:fresh --seed
undefined

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
  • 每个新模型都要配对对应的迁移和工厂类
  • 若迁移已合并到
    main
    分支,绝不要编辑它——新增一个新的迁移
  • 在功能分支中,你可以修改该分支上创建的迁移(如果尚未合并)
  • 在填充器类中填充真实但精简的数据集;大型数据集请存放在外部

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
  • 优先使用状态修饰符(如
    ->state([...])
    )而非布尔标记
  • 在工厂类中使用关联关系(如
    belongsTo
    )来构建真实的数据关联图
  • 保持工厂类执行快速;尽可能将耗时的设置移到填充器中

Testing

测试

  • Use factories in tests; avoid manual inserts
  • For integration tests touching DB, use transactions or
    RefreshDatabase
  • 在测试中使用工厂类;避免手动插入数据
  • 对于涉及数据库的集成测试,使用事务或
    RefreshDatabase
    特性