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)12.8k1[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 1mo ago

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 59% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity24

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

2986d 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

[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[aglipanci/laravel-eloquent-case

Adds CASE statement support to Laravel Query Builder.

115157.2k](/packages/aglipanci-laravel-eloquent-case)

PHPackages © 2026

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