Automatically generate Laravel factories for your models.
thedoctor0/laravel-factory-generator is a Laravel package for automatically generate laravel factories for your models..
It currently has 215 GitHub stars and 1.047.179 downloads on Packagist (latest version 1.4.4).
Install it with composer require thedoctor0/laravel-factory-generator.
Discover more Laravel packages by thedoctor0
or browse all Laravel packages to compare alternatives.
Last updated
Automatically generate factories from your existing models.
It will allow you to write tests containing your models much faster.
You can install the package via composer:
composer require thedoctor0/laravel-factory-generator --dev
For Laravel 8.x and 9.x check the v1.3.2.
For Laravel 6.x and 7.x check the v1.2.5.
To generate all factories at once, simply run this artisan command:
php artisan generate:factory
It will find all models and generate test factories based on the database structure and model relations.
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);
}
}
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\Contact;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<\App\Models\User>
*/
final 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' => faker()->name,
'username' => faker()->userName,
'email' => faker()->safeEmail,
'password' => bcrypt(faker()->password),
'company_id' => \App\Company::factory(),
'remember_token' => Str::random(10),
];
}
}
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 directory.
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
By default, your model directory structure is not taken into account, even though it has subdirectories.
You can reflect it to database/factories directory by using the --recursive option:
php artisan generate:factory --recursive
If you want you can customize the factory template to suit your needs.
To publish a factory template to resources/views/vendor/factory-generator/factory.blade.php, run the artisan command:
php artisan vendor:publish --tag="factory-generator"
The MIT License (MIT). Please see license file for more information.