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 --dev. 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. This package's last release is April 2022, so compatibility with Laravel 10/11/12 is not guaranteed — verify on your target framework before committing to it. Treat it as a legacy-maintenance tool, not a greenfield choice.
  • 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.
  • 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. 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.
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
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
adachsoft/php-code-style