TheDoctor0 / laravel-factory-generator by TheDoctor0

Automatically generate Laravel factories for your models.
441,540
171
3
Package Data
Maintainer Username: TheDoctor0
Package Create Date: 2020-09-08
Package Last Update: 2024-02-28
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-03-23 03:02:51
Package Statistics
Total Downloads: 441,540
Monthly Downloads: 23,793
Daily Downloads: 1,121
Total Stars: 171
Total Watchers: 3
Total Forks: 14
Total Open Issues: 1

Laravel Factory Generator

Packagist Packagist License

This package will generate factories from your existing models.

That way you can get started with testing your Laravel or Lumen application more quickly!

It is a forked version of mpociot/laravel-test-factory-helper package.

Installation

You can install the package via composer:

composer require thedoctor0/laravel-factory-generator --dev

For Lumen it is also required to register FactoryGeneratorServiceProvider.

Usage

To generate multiple factories at once, run the artisan command:

php artisan generate:factory

This command will find all models within your application and create test factories.

To generate a factory for only specific model or models, run the artisan command:

php artisan generate:factory User Company

By default, generation will not overwrite any existing model factories.

You can force overwriting existing model factories by using the --force option:

php artisan generate:factory --force

By default, it will search recursively for models under the app/Models (Laravel/Lumen 8.x) or app for (Laravel/Lumen 6.x and 7.x).

If your models are within a different folder, you can specify this using --dir option.

In this case, run the artisan command:

php artisan generate:factory --dir app/Models

If your models are within a different namespace, you can specify it using --namespace option.

You just need to execute this artisan command:

php artisan generate:factory --dir vendor/package/src/Models --namespace CustomNamespace\\Models

Example

Migration and Model

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('username');
    $table->string('email')->unique();
    $table->string('password', 60);
    $table->integer('company_id');
    $table->rememberToken();
    $table->timestamps();
});

class User extends Model {
    public function company()
    {
        return $this->belongsTo(Company::class);
    }
}

Generated Factory

For Laravel/Lumen 6.x and 7.x:

$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'username' => $faker->userName,
        'email' => $faker->safeEmail,
        'password' => bcrypt($faker->password),
        'company_id' => factory(App\Company::class),
        'remember_token' => Str::random(10),
    ];
});

For Laravel/Lumen 8.x:

class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition(): array
    {
        return [
            'name' => $this->faker->name,
            'username' => $this->faker->userName,
            'email' => $this->faker->safeEmail,
            'password' => bcrypt($this->faker->password),
            'company_id' => \App\Company::factory(),
            'remember_token' => Str::random(10),
        ];
    }
}

License

The MIT License (MIT). Please see license file for more information.