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

Legacy Factories Laravel Package

laravel/legacy-factories

Bring back classic Laravel model factories (<=7.x) on Laravel 8+ projects. laravel/legacy-factories restores the old factory syntax for generating test and seed data, easing upgrades and maintaining legacy codebases without rewrites.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer: composer require laravel/legacy-factories --dev. Once installed, register the service provider in config/app.php (for Laravel < 5.5, though most modern usage occurs in apps ≥5.5 where it auto-discovers). No further setup is required—your existing legacy factory definitions (e.g., database/factories/ModelFactory.php using closures and $factory->define()) should now work immediately. The factory() helper becomes available again, allowing you to instantiate models exactly as in Laravel 7 and earlier (e.g., factory(User::class)->create()). Your first use case: run an existing test suite without refactoring factory definitions—just ensure you haven’t deleted or renamed them before upgrading.

Implementation Patterns

  • Seamless migration path: Keep all legacy closures in database/factories/*.php and register them as usual. Use php artisan tinker or tests to verify they still function after upgrading Laravel core.
  • Hybrid adoption: Gradually migrate factories one-by-one to class-based factories (e.g., UserFactory::new()->make()) while leaving legacy ones in place. This package ensures both styles coexist.
  • Testing workflows: Use legacy factories in feature/unit tests unchanged—even in Laravel 9+ apps—while gradually introducing Factory classes where new functionality is added.
  • Seeder compatibility: Legacy seeders relying on factory(App\Models\User::class, 10)->create() continue to work; no need to rewrite seeder logic unless desired.
  • Custom state methods: Define legacy state closures (e.g., ->state(['name' => 'Admin'])) as before; these remain fully compatible under this package.

Gotchas and Tips

  • No Laravel 8+ factory class support: This package only restores legacy closures. It does not integrate with ModelFactory::new() or asJSON()/for()/has() helpers. Don’t expect mixed usage of both factory APIs to share definitions.
  • Factory override risk: Laravel 8+ ships with Illuminate\Database\Eloquent\Factories\Factory as the default. Ensure this package’s service provider loads after the legacy helper registration to avoid conflicts—though typically the package handles this.
  • Testing pitfalls: In orchestra/testbench-based tests, manually register the service provider if auto-discovery doesn’t fire (e.g., in setUp() or TestCase::createApplication()).
  • Deprecation warning: Though functional, legacy factories are deprecated in Laravel core and will not receive new features (e.g., async creation). Plan eventual migration.
  • Extension points: This package is minimal—no custom configuration. For deeper control, override Factory::resolve() or extend the helper manually, but doing so may break its intended simplicity. Prefer incremental refactoring over customization.
  • Debugging tip: If factory() is undefined, check composer dump-autoload and verify the package is installed in require-dev. Use php artisan optimize:clear after installation to refresh helper cache.
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