PHPackages                             dbt/model-factory - 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. [Database &amp; ORM](/categories/database)
4. /
5. dbt/model-factory

ActiveLibrary[Database &amp; ORM](/categories/database)

dbt/model-factory
=================

10.0.1(1y ago)09421MITPHPPHP ^8.1|^8.2CI failing

Since Feb 22Pushed 1y ago4 watchersCompare

[ Source](https://github.com/DeBoerTool/model-factory)[ Packagist](https://packagist.org/packages/dbt/model-factory)[ RSS](/packages/dbt-model-factory/feed)WikiDiscussions trunk Synced yesterday

READMEChangelog (10)Dependencies (9)Versions (21)Used By (0)

[![Build Status](https://camo.githubusercontent.com/2796ab8807ac318929bb5ed4ca0d6d02331c7c622859ff5fd1a81e130f498c6e/68747470733a2f2f7472617669732d63692e6f72672f4465426f6572546f6f6c2f6d6f64656c2d666163746f72792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/DeBoerTool/model-factory)[![Latest Stable Version](https://camo.githubusercontent.com/6107e176bcdc97226902a2b583822abfbce5e064c21b0e69450b92358b17f135/68747470733a2f2f706f7365722e707567782e6f72672f6462742f6d6f64656c2d666163746f72792f762f737461626c65)](https://packagist.org/packages/dbt/model-factory)[![License](https://camo.githubusercontent.com/202fedab901b178058382b1da86424d052c25521929139c3b55617b46e3de2b1/68747470733a2f2f706f7365722e707567782e6f72672f6462742f6d6f64656c2d666163746f72792f6c6963656e7365)](https://packagist.org/packages/dbt/model-factory)

Class-based Model Factories for Laravel
=======================================

[](#class-based-model-factories-for-laravel)

This package is alternative to keeping your model factories in plain PHP files.

Getting Started
---------------

[](#getting-started)

### Prerequisites

[](#prerequisites)

This package requires PHP 7.1.3 or higher, `illuminate/support@^5.7`, and `illuminate/database@^5.7`.

### Installing

[](#installing)

Via Composer:

```
composer require dbt/model-factory
```

### Testing

[](#testing)

Run:

```
composer test
```

Configuring
-----------

[](#configuring)

Publish the `model-factory.php` configuration file with `php artisan vendor:publish` command, or copy the file from this repository. The service provider should be auto-discovered by Laravel.

A model factory looks like this:

```
use Dbt\ModelFactory\ModelFactory;

class MyModelFactory extends ModelFactory
{
    protected $model = MyModel::class;

    /**
     * This is the main factory definition.
     * @return array
     */
    public function definition (): array
    {
        return [
            'my_string_column' => $this->faker->name,
        ];
    }

    /**
     * This will happen after the model is created.
     * @return void
     */
    public function after (MyModel $model): void
    {
        // Do some stuff to the model.
    }

    /**
     * This is a factory state.
     * @return array
     */
    public function myState (): array
    {
        return [
            'my_int_column' => rand(1, 10),
        ];
    }

     /**
      * This will happen after the model is created in the given state.
      * @return void
      */
     public function afterMyState (MyModel $model): void
     {
         // Do some stuff to the model.
     }
}
```

To register your model factory, include it in the config file:

```
'classes' => [
    // ...
    MyModelFactory::class,
];
```

Usage
-----

[](#usage)

### With the `factory(...)` function

[](#with-the-factory-function)

Your model factories will be registered with Laravel as usual, so they can be called with Laravel's global `factory()` function:

```
// Factory without state.
$model = factory(MyModel::class)->create();

// Factory with state.
$modelWithState = factory(MyModel::class)->states('myState')->create();
```

### With the `Create` class

[](#with-the-create-class)

If you prefer a slightly more expressive way to create models for testing, try out the `Create` class:

```
$model = Create::a(new MyModel, new Count(10), new States('myState'), new Overrides(['column' => 'value']));
```

The method definition requires a model followed by variadic `Param`s, in any order, and in any combination.

```
// Create a model with defaults.
$model = Create::a(new MyModel);

// The following are all equivalent:
Create::a(new MyModel, new Count(...), new States(...), new Overrides(...));
Create::a(new MyModel, new States(...), new Count(...), new Overrides(...));
Create::a(new MyModel, new States(...), new Overrides(...), new Count(...));

// Etc.
```

License
-------

[](#license)

MIT. Do as you wish.

###  Health Score

39

—

LowBetter than 85% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 72.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~117 days

Recently: every ~270 days

Total

18

Last Release

691d ago

Major Versions

2.0.2 → 3.0.02019-09-03

3.2.1 → 4.0.02020-09-28

4.0.0 → 5.0.02020-12-08

5.0.3 → 9.0.02022-08-25

9.0.0 → 10.0.02024-08-07

PHP version history (6 changes)1.0.0PHP &gt;=7.1.3

5.0.0PHP &gt;=7.2.5

5.0.2PHP ^8.0

9.0.0PHP &gt;=8.0.2

10.0.0PHP &gt;=8.2

10.0.1PHP ^8.1|^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/ebd3fe214b5768e700ae43c7fdc4b83d249972a6a97a0faef372d0ec19256eca?d=identicon)[danielsdeboer](/maintainers/danielsdeboer)

---

Top Contributors

[![danielsdeboer](https://avatars.githubusercontent.com/u/13170241?v=4)](https://github.com/danielsdeboer "danielsdeboer (39 commits)")[![justsanjit](https://avatars.githubusercontent.com/u/9400880?v=4)](https://github.com/justsanjit "justsanjit (9 commits)")[![kaydenvanrijn](https://avatars.githubusercontent.com/u/62964405?v=4)](https://github.com/kaydenvanrijn "kaydenvanrijn (5 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/dbt-model-factory/health.svg)

```
[![Health](https://phpackages.com/badges/dbt-model-factory/health.svg)](https://phpackages.com/packages/dbt-model-factory)
```

###  Alternatives

[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k29.9M42](/packages/kirschbaum-development-eloquent-power-joins)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8723.1M23](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.4M2](/packages/glushkovds-phpclickhouse-laravel)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[laravel-liberu/laravel-gedcom

A package that converts gedcom files to Eloquent models

782.5k1](/packages/laravel-liberu-laravel-gedcom)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
