Here are the main benefits of this package as provided in the readme:
- use the features you already love from Laravel factories (
- automatically create new class factories for specific or
- automatically import defined
statesfrom 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.
You can learn more about this package, get full installation instructions, and view the source code on GitHub at christophrumpel/laravel-factories-reloaded.