PHPackages                             skovachev/fakefactory - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. skovachev/fakefactory

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

skovachev/fakefactory
=====================

A model factory package for Laravel 4 with expressive API for creating custom tailored dummy objects

1.0.0(12y ago)291.1k2[4 issues](https://github.com/skovachev/fakefactory/issues)MITPHPPHP &gt;=5.3.0

Since Mar 16Pushed 11y ago2 watchersCompare

[ Source](https://github.com/skovachev/fakefactory)[ Packagist](https://packagist.org/packages/skovachev/fakefactory)[ RSS](/packages/skovachev-fakefactory/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Fakefactory [![Build status](https://camo.githubusercontent.com/081ee84a5e99c6cfa588c412076cea644c0a254008183b0ebda3b0b504860f40/68747470733a2f2f6170692e7472617669732d63692e6f72672f736b6f7661636865762f66616b65666163746f72792e706e67)](https://camo.githubusercontent.com/081ee84a5e99c6cfa588c412076cea644c0a254008183b0ebda3b0b504860f40/68747470733a2f2f6170692e7472617669732d63692e6f72672f736b6f7661636865762f66616b65666163746f72792e706e67)
=====================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#fakefactory-)

This is a Laravel 4 package that makes the process of generating fake data models a breeze. It will enable you to quickly generate data for your tests or just to seed your application's database for UI review.

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

[](#installation)

You'll need to add the package to you `composer.json` file.

```
"require-dev": {
    "skovachev/fakefactory": "dev-master"
}
```

Then you need to run `composer install` to download the package contents and update the autoloader.

Once it's installed you will need to register its service provider with your application. Open up `app/config/app.php` and add the following update your `providers` key.

```
'providers' => array(
    'Skovachev\Fakefactory\FakefactoryServiceProvider',
)
```

You'll also need to update your `aliases` key as well.

```
'aliases' => array(
    'Fakefactory' => 'Skovachev\Fakefactory\Facade',
)
```

That's it.

Please note that this packages requires [Faker](https://github.com/fzaninotto/Faker).

Fakefactory
-----------

[](#fakefactory)

The Fakefactory is the main class that you'll be using. It's responsible for create the fake models for your application. When it's instructed to create a model it will query the database for any information about that model's attributes and relations to other models. Based on that and any information provided by [Faker objects](#fakers) it will create a model instance.

### Usage

[](#usage)

Here are several examples as how you can use the Fakefactory to create model instances.

```
$user = Fakefactory::make('User');
```

The above example creates a `User` model instance, but without saving it to the database. To create a model and save it one go use:

```
$user = Fakefactory::create('User');
```

### Extracting model data from the database

[](#extracting-model-data-from-the-database)

The Fakefactory will try to extract any data it can about your model from the database. It will extract column names and types and based on that select appriate value generation methods. It will also be able to determine any related models based on foreign keys present on the model's table. You can edit the default value generation rules by editing the modules configuration. You'll need run the following code to publish the configration into your config folder:

```
php artisan config:publish skovachev/fakefactory

```

### ID generation

[](#id-generation)

By default the Fakefactory will not generate ID attributes for your models. This however can be changed either in the package `config.php` file or by using the following build option:

```
$user = Fakefactory::generateId()->create('User');
```

This build option can also be used as a one time override of the configuration. If you had it enabled by default you could then disable it for a single model instance:

```
$user = Fakefactory::generateId(false)->create('User');
```

### Override attributes

[](#override-attributes)

In some cases you'd want to override some attributes with specific values instead of using the fake values generated by the factory. You can do that with the `overrideAttributes` build option.

```
$user = Fakefactory::overrideAttributes(['username' => 'foobar'])->make('User');
```

Alternatively, you can provide overrides in the make / create method as well.

```
$user = Fakefactory::make('User', ['username' => 'foobar']);
```

### Generate relation models

[](#generate-relation-models)

In some cases you may want to generate a model along with a related model and insert both of them. This can be done with the `with` build option.

```
$user = Fakefactory::with('posts')->make('User');
```

In the above example the factory will generate a `Post` model and attach it to the `User` model based on their relation type. You can also override attributes of the related model by nesting the like in the following example:

```
$user = Fakefactory::with('posts')->make('User', [
	'username' => 'foobar',
	'posts' => [
		'title' => 'Fake title'
	]
]);
```

### Excluding attributes from generation

[](#excluding-attributes-from-generation)

In some cases you may want to NOT generate values for some attributes. In that case you can use the `excludeAttributes` build option.

```
$user = Fakefactory::excludeAttributes('username')->make('User');
```

The above code will generate a `User` model instance, but without providing a value for the `username` attribute.

Fakers
----------------------------------------

[](#fakers)

In some cases the Fakefactory will not able to extract all the information it needs to generate a good fake instance from the database. In other cases you'll want more control over how fake values are generated for a specific model attribute. Then you'll need to create a Faker object for your class. Faker objects provide additional information on how to generate fake models that match your business domain more closely.

### Creating fakers

[](#creating-fakers)

A simple Faker object for the `User` model may look like so:

```
class UserFaker extends \Skovachev\Fakefactory\Faker
{
    protected $attributes = [
        'type' => ['randomElement', ['admin', 'client']]
    ];

    protected $relatedTo = ['posts'];
}
```

The above code will tell the Fakefactory class that when generating a `User` model it needs to set the `type` attribute to either *admin* or *client*. Additionally it tells it that there is relation present that does not include a foreign key on the model's table. In this case the foreign key would be on the *posts* table - for instance *user\_id*.

### Faking related classes by default

[](#faking-related-classes-by-default)

You can indicate that some relations in your model should be faked by default, without you having to specify the `with` rule when creating a faked object:

```
class UserFaker extends \Skovachev\Factory\Faker
{
    protected $with = ['account'];
}
```

### Assigning fakers to classes

[](#assigning-fakers-to-classes)

Simply creating the class, however, will not be enough. You'll need to tell the Fakefactory to use this Faker class when it needs to generate `User` models. The following code registers the Faker object with the Fakefactory:

```
Fakefactory::registerClassFaker('User', 'UserFaker');
```

### Generator rules

[](#generator-rules)

The Fakefactory package uses the awesome [Faker package](https://github.com/fzaninotto/Faker) to generate fake data. It will extract column names and types from the database and use the appropriate generator rules for them. For instance if we had a column of type `Integer` the factory would use the following faker method:

```
$value = $faker->randomNumber(1, 10000);
```

If we had a column named *phone* the `phoneNumber` method would be used. All this can be changed in the package's `config.php` file.

### Custom rules

[](#custom-rules)

If you wanted to generate a custom rule for a specific attribute you'll need to have a `Faker` class with a custom method. Let's say we wanted all usernames to be in the form &lt;random\_number&gt;\_&lt;random\_string&gt;. Our Faker class could look like so:

```
class UserFaker extends \Skovachev\Fakefactory\Faker
{
    protected $attributes = [
        'username' => 'custom'
    ];

    public function getUsernameFakeValue($faker)
    {
        return $faker->randomNumber(1,1000) . '_' . $faker->lexify;
    }
}
```

The `$faker` argument is an instance of the `Faker\Generator` object providing all the generation of abilities of the [Faker package](https://github.com/fzaninotto/Faker).

You can also use any of the rules provided by the generator directly from the `attributes` array. If the `Faker\Generator` method you want to use expects arguments just pass them after the method name, separated with '|'. For example to generate 3 paragraphs of text you'll need to use the `paragraphs` method. Your attributes array may look like this:

```
protected $attributes = [
    'text' => 'paragraphs|3'
];
```

License
-------

[](#license)

Fakefactory is released under the MIT Licence. See the bundled LICENSE file for details.

Contributions
-------------

[](#contributions)

Please do not hesitate to send suggestions and feature requests. Let's make this package awesome!

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 82.8% 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

Unknown

Total

1

Last Release

4488d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1137946?v=4)[Stefan Kovachev](/maintainers/skovachev)[@skovachev](https://github.com/skovachev)

---

Top Contributors

[![skovachev](https://avatars.githubusercontent.com/u/1137946?v=4)](https://github.com/skovachev "skovachev (24 commits)")[![rhynodesigns](https://avatars.githubusercontent.com/u/2198266?v=4)](https://github.com/rhynodesigns "rhynodesigns (4 commits)")[![dylanlindgren](https://avatars.githubusercontent.com/u/6241518?v=4)](https://github.com/dylanlindgren "dylanlindgren (1 commits)")

---

Tags

laravelfactoryfakerfixturestoolmodelsseed

### Embed Badge

![Health badge](/badges/skovachev-fakefactory/health.svg)

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[venturedrake/laravel-crm

A free open source CRM built as a package for laravel projects

42010.0k](/packages/venturedrake-laravel-crm)[xefi/faker-php-laravel

Faker php integration with laravel

2618.0k](/packages/xefi-faker-php-laravel)

PHPackages © 2026

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