The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

Laravel Factory Generator

thedoctor0/laravel-factory-generator image

Laravel Factory Generator stats

Downloads
329.1K
Stars
159
Open Issues
1
Forks
13

View on GitHub →

Automatically generate Laravel factories for your models.

Laravel Factory Generator

Automatically generate 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

For Laravel 6.x and 7.x check the v1.2.5.

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\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),
];
}
}

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.

TheDoctor0 photo

Full-Stack Developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Thedoctor0 Laravel Factory Generator Related Articles

Handling Geospatial Data with Laravel Magellan image

Handling Geospatial Data with Laravel Magellan

Read article
Laravel 10.32 Released image

Laravel 10.32 Released

Read article
Learn all about Laravel's dependency injection container image

Learn all about Laravel's dependency injection container

Read article
Laravel 8 is Now Released! image

Laravel 8 is Now Released!

Read article
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu