Download the PHP package christophrumpel/laravel-factories-reloaded without Composer

On this page you can find all versions of the php package christophrumpel/laravel-factories-reloaded. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-factories-reloaded

Laravel Factories Reloaded 🏭

Latest Version on Packagist v1 Total Downloads

This package generates class-based model factories, which you can use instead of the ones provided by Laravel.

Screenshot of the command

Laravel 8 Support

The new version v3 now supports Laravel 8 and PHP 8+. Since L8 changed their own implementation of factories a lot, this new v2 version ONLY works with Laravel 8. If you are not using L8 yet, use the latest 1. version of this package, if you need PHP 7 support, use the latest 2. version.

Benefits

📺 I've recorded some videos to give you an overview of the features.

:warning: Note: Interested in WHY you need class-based factories? Read here.

Installation

You can install the package via composer:

To publish the config file run:

It will provide the package's config file where you can define multiple paths of your models, the path of the newly generated factories, the namespace of your old Laravel factories, as well as where your old Laravel factories are located.

Configuration

Laravel Factories Namespace

Factories are namespaced since Laravel 8, and the default factories namespace is Database\Factories. If your laravel factories namespace is Database\Factories\ClassFactories, you can customize it at the config file as below:

It will resolve your old laravel factory class as Database\Factories\ClassFactories\UserFactory, instead of Database\Factories\UserFactory.

Usage

Generate Factories

First, you need to create a new factory class for one of your models. This is done via a newly provided command called make:factory-reloaded.

You can pick one of the found models or create factories for all of them.

Command Options

If you want to define options through the command itself, you can do that as well:

Currently, you can only define one location for your models this way.

Define Default Model Data

Similar to Laravel factories, you can define default data for your model instances. Inside your new factories, there is a getDefaults method defined for that. The Faker helper to create dummy data is available as well.

Use New Factories

Let's say you have created a new user factory. You can now start using it and create a new user instance. Similar to Laravel factories, the create method will persist in a new model.

If you like to get an instance that is not persisted yet, you can choose the make method.

To create multiple instances, you chain the times() method before the create or make method.

States

You may have defined states in your old Laravel factories.

While creating a new class factory, you will be asked if you like those states to be imported to your new factories. If you agree, you can immediately use them. The state active is now a method on your UserFactory.

Relations

Often you will need to create a new model instance with related models. This is now pretty simple by using the with method:

Here were are getting a user instance that has three related recipes attached. The second argument here defines the relationship name.

:warning: Note: For this to work, you need to have a new RecipeFactory already created.

You can also define extras for the related models when using related model factories directly.

You can create many related models instances by chaining withs.

Here we are getting a recipe that has a group and four ingredients.

:warning: Note: Up to the version 1.0.8, only the last with relation is built.

In Laravel factories, you could also define a related model in your default data like:

This can also be achieved in our new factory classes.

Or even better through an instance of a new factory class.

:warning: Note: I wouldn't recommend any of these options because you do not see that additional models are persisted in your tests. Please use the given "with" method create a dedicated method for creating a relation yourself.

Callbacks

In Laravel, you are able to define factory callbacks for afterCreating and afterMaking. You can do something similar also with factory classes. Since both the make and create method are inside your factory class, you are free to add code there:

It depends on what you want to achive, but personally I would add a method to your factory which you call from within your test. This way it is more obvious what is happening.

Immutability

You might have noticed that when this package imports a state for you, it will clone the factory before returning.

This is recommended for all methods which you will use to setup your test model. If you wouldn't clone the factory, you will always modify the factory itself. This could lead into problems when you use the same factory again.

To make a whole factory immutable by default, set the $immutable property to true. That way, every state change will automatically return a cloned instance.

In some context, you might want to use a standard factory as immutable. This can be done with the immutable method.

Note: with and withFactory methods are always immutable.

What Else

The best thing about those new factory classes is that you own them. You can create as many methods or properties as you like to help you create those specific instances that you need. Here is how a more complex factory call could look like:

Using such a factory call will help your tests to stay clean and give everyone a good overview of what is happening here.

Why Class-Based Factories?

Testing

Changelog

Please see CHANGELOG for more information about what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

Credits

Some of the implementations are inspired by Brent's article about how they deal with factories at Spatie.

And a big thanks goes out to Adrian who helped me a lot with refactoring this package.

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-factories-reloaded with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
christophrumpel/laravel-command-file-picker Version ^1.1
illuminate/support Version ^8.0
laravel/framework Version ^8.0
nikic/php-parser Version ^4.3
roave/better-reflection Version ^5.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package christophrumpel/laravel-factories-reloaded contains the following files

Loading the files please wait ....