PHPackages                             analogue/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. analogue/factory

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

analogue/factory
================

Implementation of Illuminate database factory for Analogue orm

v1.3.1(8y ago)13.4k1[1 issues](https://github.com/analogueorm/factory/issues)1MITPHPPHP &gt;=7.0

Since Jun 8Pushed 8y ago1 watchersCompare

[ Source](https://github.com/analogueorm/factory)[ Packagist](https://packagist.org/packages/analogue/factory)[ Docs](http://github.com/analogueorm/factory)[ RSS](/packages/analogue-factory/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (8)Versions (15)Used By (1)

Analogue ORM - Entity Factory
=============================

[](#analogue-orm---entity-factory)

This package adds support the laravel's Model Factory' functionnality to the Analogue datamapper ORM.

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

[](#installation)

Add this line to your composer.json file :

```
composer require analogue/factory

```

Configuration
-------------

[](#configuration)

Add the Service Provider to config/app.php :

```
'Analogue\Factory\FactoryServiceProvider',

```

Usage
-----

[](#usage)

Analogue Factory uses the same definitions mechanism Eloquent's does. Out of the box, this file provided with the default Laravel's install contains one factory definition:

```
$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
        'remember_token' => str_random(10),
    ];
});

```

Within the Closure, which serves as the factory definition, you may return the default test values of all attributes on the model. The Closure will receive an instance of the [Faker](https://github.com/fzaninotto/Faker) PHP library, which allows you to conveniently generate various kinds of random data for testing.

Of course, you are free to add your own additional factories to the `ModelFactory.php` file.

### Multiple Factory Types

[](#multiple-factory-types)

Sometimes you may wish to have multiple factories for the same Analogue Entity class. For example, perhaps you would like to have a factory for "Administrator" users in addition to normal users. You may define these factories using the `defineAs` method:

```
$factory->defineAs(App\User::class, 'admin', function ($faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => str_random(10),
        'remember_token' => str_random(10),
        'admin' => true,
    ];
});

```

Instead of duplicating all of the attributes from your base user factory, you may use the `raw` method to retrieve the base attributes. Once you have the attributes, simply supplement them with any additional values you require:

```
$factory->defineAs(App\User::class, 'admin', function ($faker) use ($factory) {
    $user = $factory->raw(App\User::class);

    return array_merge($user, ['admin' => true]);
});

```

### Using Factories In Tests

[](#using-factories-in-tests)

Once you have defined your factories, you may use them in your tests or database seed files to generate model instances using the global `analogue_factory` function. So, let's take a look at a few examples of creating models. First, we'll use the `make` method, which creates models but does not save them to the database:

```
public function testDatabase()
{
    $user = analogue_factory(App\User::class)->make();

    // Use entity in tests...
}

```

If you would like to override some of the default values of your entities, you may pass an array of values to the `make` method. Only the specified values will be replaced while the rest of the values remain set to their default values as specified by the factory:

```
$user = analogue_factory(App\User::class)->make([
    'name' => 'Abigail',
   ]);

```

You may also create a Collection of many models or create models of a given type:

```
// Create three App\User instances...
$users = analogue_factory(App\User::class, 3)->make();

// Create an App\User "admin" instance...
$user = analogue_factory(App\User::class, 'admin')->make();

// Create three App\User "admin" instances...
$users = analogue_factory(App\User::class, 'admin', 3)->make();

```

### Persisting Factory Entities

[](#persisting-factory-entities)

The `create` method not only creates the model instances, but also saves them to the database using Analogue's Mapper store() method.

```
public function testDatabase()
{
    $user = analogue_factory(App\User::class)->create();

    // Use entity in tests...
}

```

Again, you may override attributes on the model by passing an array to the `create` method:

```
$user = analogue_factory(App\User::class)->create([
    'name' => 'Abigail',
   ]);

```

### Building complex Entities

[](#building-complex-entities)

By recursively calling the `analogue_factory` function, you can generate complex Entities very easily :

```
$factory->define(App\Post::class, function (Faker\Generator $faker) {
    return [
        'title' => $faker->sentence,
        'content' => $faker->text,
    ];
});

$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'posts' => analogue_factory(App\Post::class, 10),
    ];
});

```

You can even persist these complex entities with a single call :

```
$users = analogue_factory(App\User::class, 3)->create();

```

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 93.9% 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 ~49 days

Recently: every ~96 days

Total

14

Last Release

3032d ago

Major Versions

0.1.x-dev → v1.02016-06-08

0.2.x-dev → v1.0.12016-12-22

PHP version history (4 changes)0.1.x-devPHP &gt;=5.5.0

v0.2PHP &gt;=5.6.4

v1.1PHP &gt;=7.0.0

v1.3PHP &gt;=7.0

### Community

Maintainers

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

---

Top Contributors

[![RemiCollin](https://avatars.githubusercontent.com/u/9589616?v=4)](https://github.com/RemiCollin "RemiCollin (31 commits)")[![smallhadroncollider](https://avatars.githubusercontent.com/u/477286?v=4)](https://github.com/smallhadroncollider "smallhadroncollider (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[glushkovds/phpclickhouse-laravel

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

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[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)
