thedoctor0/laravel-factory-generator
| Install | |
|---|---|
composer require thedoctor0/laravel-factory-generator |
|
| Latest Version: | 1.5.0 |
| PHP: | ^8.2 |
| Laravel: | 11 · 12 · 13 |
| Older releases: | 6 – 10 |
| License: | MIT |
| Last Updated: | Aug 10, 2026 |
| Links: | GitHub · Packagist |
Laravel Factory Generator
Automatically generate class-based factories from your existing models.
It will allow you to write tests containing your models much faster.
Installation
You can install the package via composer:
composer require thedoctor0/laravel-factory-generator --dev
Version support
| Package | Laravel | PHP |
|---|---|---|
| 2.x | 10.x - 13.x | 8.1+ |
| v1.3.2 | 8.x - 9.x | 7.3+ |
| v1.2.5 | 6.x - 7.x | 7.2+ |
Usage
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.
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
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\User;
use Illuminate\Support\Str;
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' => fake()->name,
'username' => fake()->userName,
'email' => fake()->safeEmail,
'password' => bcrypt(fake()->password),
'company_id' => \App\Models\Company::factory(),
'remember_token' => Str::random(10),
];
}
}
Nullable columns use fake()->optional(), related models resolve to their own ::factory() calls, and the customizable template controls the final shape.
Advanced usage
Selecting models
To generate a factory for only specific model or models, run the artisan command:
php artisan generate:factory User Company
Overwriting existing factories
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
Customizing the output directory
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
Customizing the namespace
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
Using recursive mode
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
Customizing the factory template
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"
License
The MIT License (MIT). Please see license file for more information.
Related Packages
Generate Laravel test factories from your existing models
The goal of this package is to enable the rapid creation of objects for the purp...
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 1.5.0 | ^8.2 | ^11.0| | MIT | |
| 1.4.5 | ^8.1 | ^10.0| | MIT | |
| 1.4.4 | ^8.1 | ^10.0| | MIT | |
| 1.4.3 | ^8.1 | ^10.0| | MIT | |
| 1.4.2 | ^8.1 | ^10.0| | MIT | |
| 1.4.1 | ^8.1 | ^10.0| | MIT | |
| 1.4.0 | ^8.1 | ^10.0| | MIT | |
| 1.3.3 | >=7.4 | ^8.0| | MIT | |
| 1.3.2 | >=7.4 | ^8.0| | MIT | |
| 1.3.1 | >=7.4 | ^8.0| | MIT | |
| 1.3.0 | >=7.4 | ^8.0| | MIT | |
| 1.2.5 | >=7.4 | ^6.0| | MIT | |
| 1.2.4 | >=7.4 | ^6.0| | MIT | |
| 1.2.3 | >=7.2 | ^6.0| | MIT | |
| 1.2.2 | >=7.2 | ^6.0| | MIT | |
| 1.2.1 | >=7.2 | ^6.0| | MIT | |
| 1.2.0 | >=7.2 | ^6.0| | MIT | |
| 1.1.0 | >=7.2 | ^6.0| | MIT | |
| 1.0.6 | >=7.2 | ^6.0| | MIT | |
| 1.0.5 | >=7.2 | ^6.0| | MIT | |
| 1.0.4 | >=7.2 | ^6.0| | MIT | |
| 1.0.3 | >=7.2 | ^6.0| | MIT | |
| 1.0.2 | ^7.2.5 | ^6.0| | MIT | |
| 1.0.1 | ^7.2.5 | ^6.0| | MIT | |
| 1.0.0 | ^7.2.5 | ^6.0| | MIT |
