Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Foundry Laravel Package

zenstruck/foundry

Zenstruck Foundry supercharges Laravel/Symfony testing with fluent model factories, fixtures, and story-based data builders. Create, persist, and customize entities easily, manage relations, and write cleaner, faster tests with powerful helpers and states.

Deep Wiki
Context7

zenstruck/foundry is a fixtures and test data factory toolkit for PHP (especially Symfony) that makes creating realistic objects fast, expressive, and maintainable. It streamlines seeding and testing by providing fluent factories, sensible defaults, and convenient helpers for persistence and relationships.

Use it to generate consistent datasets for unit/functional tests and local development without drowning in boilerplate.

  • Factory classes with fluent APIs and overridable defaults
  • Persistence integration (e.g., Doctrine) with easy create/find helpers
  • Faker-powered data generation for realistic fixtures
  • Relationships & states to model complex object graphs
  • Utilities for seeding, resetting, and test ergonomics
Frequently asked questions about Foundry
How does zenstruck/foundry compare to Laravel’s built-in Factory classes?
Foundry offers more expressive syntax and stateful fixtures, reducing boilerplate in complex test scenarios. While Laravel’s factories work well for simple cases, Foundry’s fluent API and auto-completion for relationships (e.g., User → Post → Comment) make it ideal for large-scale testing. You can coexist with Laravel’s factories or replace them entirely, depending on your needs.
Can I use zenstruck/foundry with Laravel’s Eloquent models?
Yes, Foundry works seamlessly with Eloquent since it’s built on Doctrine ORM principles. You’ll need to wrap Foundry’s Factory in a Laravel-friendly facade or trait (e.g., `HasFoundry`) to integrate with Eloquent’s `create()` or `factory()` methods. The stateful approach also aligns well with Laravel’s testing conventions.
Does zenstruck/foundry support Laravel’s RefreshDatabase or MigrateFresh?
Foundry’s stateful fixtures integrate well with Laravel’s database testing traits. You can use Foundry’s `Loader` to register fixtures before tests run, ensuring clean state management. For `RefreshDatabase`, Foundry’s fixtures will regenerate data on each test cycle, while `MigrateFresh` works similarly but with fresh migrations.
How do I install and set up zenstruck/foundry in a Laravel project?
Install via Composer: `composer require zenstruck/foundry`. Register the `FoundryServiceProvider` in `config/app.php` or use the `Loader` in your test `setUp()` method. Define factories in PHP classes (e.g., `UserFactory`) and use Foundry’s fluent API to generate test data, like `$user = UserFactory::createOne().`
What Laravel versions does zenstruck/foundry support?
Foundry is compatible with Laravel 10.x+ (PHP 8.1+) and follows Laravel’s LTS releases. It leverages Doctrine ORM, which Eloquent relies on, so there are no major version conflicts. Check the Foundry docs for exact PHP/Doctrine version requirements, as they may evolve with Symfony updates.
How do I handle relationships between models in zenstruck/foundry?
Foundry simplifies relationships with auto-completion and stateful helpers. For example, `UserFactory::createOne()->withPosts(3)` generates a user with 3 related posts. You can also define custom states (e.g., `hasPublishedPost()`) to model complex object graphs. This reduces manual fixture setup compared to Laravel’s native factories.
Can zenstruck/foundry generate realistic test data without Faker?
Foundry is Faker-powered by default, but you can customize or replace the data generation logic. For non-Faker setups, override the factory’s `define()` method or use Foundry’s `State` system to inject hardcoded or deterministic values. This flexibility works well for testing edge cases or controlled environments.
Is zenstruck/foundry suitable for large-scale projects with 1000+ fixtures?
Foundry is optimized for on-demand fixture generation, avoiding global state pollution. However, deep nested fixtures (e.g., 1000+ related records) may introduce overhead. Benchmark performance in your CI pipeline and consider lazy-loading fixtures or splitting them into modular factories to maintain speed.
How do I test database migrations with zenstruck/foundry?
Foundry doesn’t natively support migration testing, but you can integrate it with tools like `laravel-shift/database` or custom test helpers. Use Foundry to generate test data *after* migrations run, then assert schema changes separately. For example, run migrations with `Artisan::call('migrate')`, then use Foundry to populate tables for validation.
Are there alternatives to zenstruck/foundry for Laravel test data?
Laravel’s built-in Factory classes and packages like `laravel-shift/database` or `mockery` are alternatives. Foundry stands out for its stateful, fluent API and auto-completion, which reduce boilerplate in complex tests. If you need simpler setups, Laravel’s factories may suffice, but Foundry excels for large projects or teams requiring maintainable, expressive fixtures.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport