- Can I use zenstruck/foundry with Laravel instead of Symfony?
- Foundry is designed for Symfony and Doctrine, but it can be used in Laravel through manual integration with Doctrine ORM or community wrappers. It is not an Eloquent-first Laravel package, so you should expect extra setup compared with Laravel's native factories. It works best in Laravel projects that already use Doctrine for core entities or want Foundry's expressive factory states.
- How do I install zenstruck/foundry in a Laravel application?
- Install it with Composer alongside Doctrine ORM and doctrine/doctrine-bundle, then configure Doctrine in Laravel according to your preferred integration. Foundry's Symfony bundle auto-configuration does not apply directly to Laravel, so you generally wire services and configuration manually. Verify the package's PHP and Symfony component constraints against your Laravel version before adopting it.
- Does zenstruck/foundry work with Eloquent models or only Doctrine ORM?
- Foundry is built around Doctrine ORM and MongoDB ODM, so features like auto-refresh and relationship persistence target Doctrine models. You can wrap Eloquent models in custom Foundry factories, but you lose some Doctrine-specific behavior and need additional factory code. For Eloquent-only applications, Laravel's native factories are usually simpler and more feature-complete.
- How does zenstruck/foundry compare to Laravel factories?
- Foundry provides fluent state methods, auto-completion, on-demand random data, lifecycle hooks, and Doctrine fixture integration. Laravel factories are Eloquent-first and tightly integrated with make:factory, the Factory facade, and Laravel seeders. Foundry can replace Laravel factory calls in Doctrine-backed apps, but migrating requires adapting syntax, persistence, and test setup.
- How do I create a model factory with zenstruck/foundry?
- Create a factory class that extends Foundry's Factory and define a defaults() method for random field data plus state methods such as published(). You can then call PostFactory::new()->published()->create(['slug' => 'post-a']) to persist a model with only the required data. Use make() instead of create() when you need an unsaved object, and rely on state methods for reusable fixture variations.
- Can zenstruck/foundry be used for database seeding and DoctrineFixturesBundle?
- Yes, Foundry factories can be used inside DoctrineFixturesBundle to load fixtures or inside tests where Foundry offers additional features. In Laravel, you would typically call the factories from seeders or test setup instead of using DoctrineFixturesBundle. Use create() for persisted fixtures and make() for in-memory objects during tests.
- How should I reset the database between Foundry tests in Laravel?
- Foundry v2+ deprecates the ResetDatabase trait in favor of built-in reset mechanisms, but Laravel projects still need to coordinate with DatabaseMigrations, DatabaseTransactions, or RefreshDatabase. Avoid mixing reset strategies that conflict with Doctrine auto-refresh or transaction handling. Test isolation carefully and use withoutDoctrineEvents() when Doctrine lifecycle events interfere with your assertions.
- Does zenstruck/foundry support PHPUnit and Pest?
- Foundry works with PHPUnit, and Symfony integration is provided through ZenstruckFoundryBundle. Pest support comes through community packages or adapters such as pestphp/foundry rather than being an official Foundry requirement. In Laravel, you can use PHPUnit or Pest as long as Doctrine persistence and Foundry configuration are set up correctly.
- What performance issues should I watch with Foundry auto-refresh?
- Doctrine auto-refresh is enabled by default and can slow down tests that create large datasets or many related objects. Disable it with auto_refresh: false when you do not need automatic relationship consistency. Also scope Faker seeds per test case rather than relying only on a global FOUNDRY_FAKER_SEED to avoid flaky tests.
- Is zenstruck/foundry maintained and suitable for production?
- Foundry is a mature Composer package intended for development and test fixtures, not for production runtime data creation. It has documented releases and upgrade guides, but you should check current Packagist activity and version constraints before adopting it. For production data, use migrations, seeders, or application services rather than test fixture factories.