PHPackages                             thedoctor0/laravel-factory-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. thedoctor0/laravel-factory-generator

ActiveLibrary[Testing &amp; Quality](/categories/testing)

thedoctor0/laravel-factory-generator
====================================

Automatically generate Laravel factories for your models.

1.4.4(1y ago)214997.4k—4.8%18[1 PRs](https://github.com/TheDoctor0/laravel-factory-generator/pulls)1MITPHPPHP ^8.1

Since Sep 8Pushed 1y ago2 watchersCompare

[ Source](https://github.com/TheDoctor0/laravel-factory-generator)[ Packagist](https://packagist.org/packages/thedoctor0/laravel-factory-generator)[ Fund](https://www.paypal.me/thedoctor0)[ GitHub Sponsors](https://github.com/thedoctor0)[ RSS](/packages/thedoctor0-laravel-factory-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (26)Used By (1)

Laravel Factory Generator
=========================

[](#laravel-factory-generator)

[![Packagist](https://camo.githubusercontent.com/8d92c6116792dc82e0978bf78c437c273e03ff9fa33a45b261bd5858eedebf6a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f546865446f63746f72302f6c61726176656c2d666163746f72792d67656e657261746f722e737667)](https://packagist.org/packages/TheDoctor0/laravel-factory-generator)[![Packagist](https://camo.githubusercontent.com/12c70061d72c47938b91405b5ebda3a86baeb8a2f2ace46a86ea50212b24963a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f546865446f63746f72302f6c61726176656c2d666163746f72792d67656e657261746f722e737667)](https://packagist.org/packages/TheDoctor0/laravel-factory-generator)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://github.com/TheDoctor0/laravel-factory-generator/blob/master/LICENSE.md)

![Banner](https://camo.githubusercontent.com/ff9e7165446160a6770fecf4a2b52720843cf637760acb0a34808787dfe17485/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c253230466163746f727925323047656e657261746f722e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d746865646f63746f72302532466c61726176656c2d666163746f72792d67656e657261746f722b2d2d646576267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d4175746f6d61746963616c6c792b67656e65726174652b746573742b666163746f726965732b666f722b616c6c2b796f75722b6d6f64656c73266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)

Automatically generate [factories](https://laravel.com/docs/master/database-testing#writing-factories) from your existing models.

It will allow you to write tests containing your models much faster.

Installation
------------

[](#installation)

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](https://github.com/TheDoctor0/laravel-factory-generator/tree/laravel-9).

For Laravel 6.x and 7.x check the [v1.2.5](https://github.com/TheDoctor0/laravel-factory-generator/tree/laravel-7).

Usage
-----

[](#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

[](#example)

#### Migration and Model

[](#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

[](#generated-factory)

```
