Weave 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

Laravel Factories Reloaded Laravel Package

christophrumpel/laravel-factories-reloaded

Laravel Factories Reloaded generates model factories from your existing database schema. It inspects tables and column types, then creates ready-to-use factory definitions with appropriate fake data, helping you skip manual boilerplate and start testing faster.

View on GitHub
Deep Wiki
Context7

Getting Started

  • Install as a dev dependency: composer require christophrumpel/laravel-factories-reloaded:^3.0 --dev. v3.0.0 is a major bump that adds PHP 8 support, so check your php constraint first — if you're still on PHP 7.x you must stay on the v2 line. The service provider is auto-discovered; on older Laravel versions without discovery, register ChristophRumpel\LaravelFactoriesReloaded\LaravelFactoriesReloadedServiceProvider in config/app.php.
  • Publish the config (php artisan vendor:publish --provider="ChristophRumpel\LaravelFactoriesReloaded\LaravelFactoriesReloadedServiceProvider") and read it once — it controls the namespace and path where factories are generated, which is the thing you'll most often need to change later.
  • Confirm the artisan generator is available with php artisan list (the package registers a make:factory-style command). Generate your first factory for a small model, e.g. User.
  • Open the generated class. It extends the package's BaseFactory, exposes $this->faker, and expects you to implement definition(): array plus point it at a model class.
  • First use case: migrate one existing closure factory. Move the array returned by $factory->define(User::class, ...) into definition(), then in a test replace factory(User::class)->create() with UserFactory::new()->create(). That one-file migration tells you everything about whether the package fits your codebase.
  • Read the README's "States" and "Relationships" sections before writing anything non-trivial — those two areas are where the API diverges most from both legacy closures and Laravel 8+ native factories.

Implementation Patterns

  • Basic definition: Return an array from definition() using $this->faker for fake values. This is a straight port of an old closure body, and for simple factories it's the whole feature.
  • States as methods: Instead of string keys, add public methods that call $this->state([...]) and return $this. Usage becomes UserFactory::new()->asAdmin()->create(), which is self-documenting, IDE-discoverable, and greppable.
  • Relationships: Return a factory instance inside definition() (e.g. 'user_id' => UserFactory::new()). On create() this resolves to a persisted parent; on make() you get an unsaved instance. This nested-resolution behavior is the main reason teams reached for this package over closures.
  • Counting and raw data: Use ->times(n) for bulk creation in seeders/tests, and ->raw() when you need the attribute array without touching the database — handy for building JSON payloads or asserting on request bodies.
  • Test workflow: Use factories directly at the call site: $this->actingAs(UserFactory::new()->create()). For seeders, combine times() with state methods (UserFactory::new()->asAdmin()->times(3)->create()).
  • Overrides: Pass an array override to create()/make() to merge on top of definition(). Combine state first, then overrides — that ordering matches developer intuition and makes intent clear.
  • Shared helpers via the base class: Subclass BaseFactory (or add a project-level AppFactory extending it) for cross-cutting needs like tenant scoping, forUser() relation helpers, or timestamp overrides. Doing this per-factory creates drift fast.

Gotchas and Tips

  • You probably don't need this on Laravel 8+. Native class-based model factories do the same job with first-party support and ongoing maintenance. v3.0.0 only adds PHP 8 support — it is not a framework-compatibility refresh, so Laravel 10/11/12 support is still not guaranteed. Verify composer why-not / resolution against your target framework before committing. Treat it as a legacy-maintenance tool, not a greenfield choice.
  • v3.0.0 is a breaking floor, not a feature release. The bump raises the PHP requirement to 8.0; nothing in the factory API changes. In practice that means: PHP 7.4 apps stay on ^2.0, PHP 8 apps can move to ^3.0, and because it's a major tag you should expect the usual composer constraint churn rather than a drop-in composer update. Budget a CI run, not a code review.
  • Don't run two factory APIs side by side. If any test still uses factory(Model::class), converting piecemeal leaves two parallel DSLs in one repo. Pick one per project (or per domain) and finish the migration.
  • Faker is on $this->faker, not fake(). This is the most common muscle-memory miss for anyone arriving from Laravel 8+, and the resulting error ("call to undefined function fake()" or a null property) is not always obvious.
  • State methods must return $this. Forgetting the return produces a confusing failure at the next chained call rather than at the state method itself.
  • make() vs create() with nested factories produce different object states. Leaking an unsaved nested model into a database assertion is a classic silent failure — verify with raw() and a short unit test before trusting a relationship factory.
  • Check times() semantics for your pinned version, especially whether it applies to make() and raw() or only create(). Assert on the returned collection size rather than assuming — and re-run that assertion after any major bump rather than trusting it carried over.
  • Namespace changes break generation. If you move the factory namespace or directory, update the package config at the same time or subsequent make:factory calls won't find the right place.
  • Debugging: In Tinker, UserFactory::new()->raw() shows the fully resolved attribute array including nested factories — much faster than reading a stack trace from a failed create().
  • Auto-discovery can mask registration issues. If the generator command "is not defined," explicitly registering the provider is the first thing to try, before hunting for version mismatches.
  • Pin the version in composer.json for legacy apps — now explicitly across the ^2/^3 boundary. On an unmaintained package, a stray minor bump can shift resolution behavior in ways that only show up in slow CI failures.
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.
hsluv/hsluv
seampact/codegen
seampact/hooks
seampact/ir
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support