Laravel Factories Reloaded

Laravel Factories Reloaded

Laravel Factories Reloaded is a package by Christoph Rumpel that generates class-based model factories that you can use instead of the factory files Laravel provides.

Here are the main benefits of this package as provided in the readme:

  • use the features you already love from Laravel factories (create, make, times, states)
  • automatically create new class factories for specific or All your models
  • automatically import defined default data and states from your Laravel factories
  • and many more…

Here are some examples of how you might use the class-based factories provided by this package:

// Create a new record in the database
$user = UserFactory::new()->create();

// Make a new model without peristing to the database
$user = UserFactory::new()->make();

// Use a factory state
$recipe = UserFactory::new()
    ->active()
    ->create();

// Relations
$recipe = RecipeFactory::new()
    ->with(Group::class, 'group')
    ->with(Ingredient::class, 'ingredients')
    ->with(Ingredient::class, 'ingredients', 3)
    ->create();

You might be asking, “what is the benefit of using class-based factories?” The project’s readme mentions the following benefits of using this package:

  • They give you much more flexibility on how to create your model instances.
  • They make your tests much cleaner because you can hide complex preparations inside the class.
  • They provide IDE auto-completion, which you do not have with Laravel factories.

Learn More

You can learn more about this package, get full installation instructions, and view the source code on GitHub at christophrumpel/laravel-factories-reloaded.

Filed in: News
Laravel News Partners

Laravel Jobs

Backend Software Engineer (PHP Developer)
Austin TX, Bonita Springs FL
Certified eSupport Corp
Backend PHP Developer
Hamburg, Germany
ABOUT YOU GmbH
Full-Stack Engineer (Mid to Senior)
Remote (EST, CST)
Voxie
Senior Laravel Developer (AI Applications)
Remote from anywhere in the UK or from our London office
GreenShoot Labs
Full Stack Engineer
Remote
Shelterluv

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.