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.
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.
database/factories/*.php and register them as usual. Use php artisan tinker or tests to verify they still function after upgrading Laravel core.UserFactory::new()->make()) while leaving legacy ones in place. This package ensures both styles coexist.Factory classes where new functionality is added.factory(App\Models\User::class, 10)->create() continue to work; no need to rewrite seeder logic unless desired.->state(['name' => 'Admin'])) as before; these remain fully compatible under this package.ModelFactory::new() or asJSON()/for()/has() helpers. Don’t expect mixed usage of both factory APIs to share definitions.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.setUp() or TestCase::createApplication()).Factory::resolve() or extend the helper manually, but doing so may break its intended simplicity. Prefer incremental refactoring over customization.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.How can I help you explore Laravel packages today?