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 Factory Generator Laravel Package

thedoctor0/laravel-factory-generator

Laravel Factory Generator automatically creates model factories from your existing models and database schema. It inspects columns and relationships to scaffold realistic factories, saving time when testing and seeding Laravel applications.

View on GitHub
Deep Wiki
Context7

Getting Started

composer require thedoctor0/laravel-factory-generator --dev
php artisan vendor:publish --provider="TheDoctor0\LaravelFactoryGenerator\FactoryGeneratorServiceProvider"

Then run the generator against a live database connection:

php artisan generate:factory

Where to look first: the README now ships a version table and modern (Laravel 8+) factory syntax examples — use these as the reference output. Confirm the generated database/factories/ files match the class-based public function definition(): array style, not the legacy $factory->define() closure style.

First use case: point it at an existing or legacy schema (or a freshly-migrated local DB) and generate factories in bulk so you can immediately write feature tests and seeders without hand-writing every definition(). Verify your Laravel/PHP version against the new README table before assuming generated code will match your project's factory conventions.

Implementation Patterns

  • Generate → curate → commit. Treat output as a scaffold. Run the generator, then hand-edit the result: tighten enums to realistic values, add state() methods, and wire up relationships the generator can't infer. Commit the curated factories, not the raw output.
  • Preview with --dry-run (new in 1.5.0). Use it in CI or on unfamiliar schemas to see what would be produced/overwritten before touching database/factories/. This is the safest way to run the tool in an existing codebase.
  • Regenerate per-table after schema changes. Rather than regenerating everything, run against the connection/table you changed and diff the result against your curated version.
  • Enum columns as states. The hardened enum driver (1.5.0) produces better candidate values. Follow up by converting meaningful enums into explicit state() methods (->state(UserState::Suspended)) instead of leaving raw enum samples in definition().
  • Modern Faker output. Generated factories now use current Faker idioms (fake()->... / fake()->unique() style). If your project still pins older Faker conventions, normalize the output or align your Faker version.
  • Test-suite-first workflow. Run the generator once, then use Model::factory()->create() in Pest/PHPUnit tests immediately. Curate incrementally as tests surface missing columns or bad sample data.
  • Connection-scoped generation. Point it at a specific connection (e.g. a migrated test DB) rather than your production-linked local DB so generated samples reflect the schema you actually test against.

Gotchas and Tips

  • Missing database/factories/ directory. Previously crashed if the directory didn't exist; 1.5.0 fixes this. If you're on <1.5.0, mkdir -p database/factories first, or upgrade.
  • It cannot infer relationships. Foreign keys become plain integer/ID faker values, not Model::factory() references. Always replace *_id columns with real factory relationships in definition(), and add configure()/afterCreating() where necessary.
  • Overwrites destroy curation. Anything that writes files (--force-style options) will clobber your hand-edited factories. Gate destructive regeneration behind --dry-run review and keep curated files in version control so diffs are visible.
  • Schema must be live. The generator introspects the actual DB, so run migrations first. Enum handling is driver-sensitive — 1.5.0 hardened the enum driver, but verify output on MySQL/MariaDB/Postgres enums rather than assuming parity.
  • Requires a working connection at generation time. Don't wire it into build steps that run before the DB is available; keep it as an on-demand dev command (--dev dependency).
  • Version table matters for syntax. The new README maps package versions to generated factory syntax. If you're on an older Laravel, pin an older package version rather than post-processing legacy syntax by hand.
  • Extension points: the config file controls output paths and naming; adjust there rather than moving generated files. Custom column-type mappings are the place to improve enum/JSON/set handling for non-standard schemas.
  • First run on a large schema: generate into a temporary output path (or via --dry-run), cherry-pick the tables you need, and avoid dumping dozens of low-quality factories you'll never maintain.
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.
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
shitoudev/permissionmanager
johannesclimacus/laravel-tui
hocvt/seotools
vitalybaev/sypexgeo
amphibee/themosis-ignition
gito-me/fast-excel
frankyso/laravel-ipaymu
geolize-studio/laravel-chile
boomerangui/chat
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core