GitHub Repository



Factories (are a short name for Models Factories).

Factories are used to generate some fake data with the help of Faker to be used for testing purposes.

Factories are mainly used from Tests.



Folder Structure

 - app
    - Containers
        - {container-name}
             - Data
                - Factories
                    - UserFactory.php
                    - ...

Code Samples

A User Model Factory:


// User
$factory->define(App\Containers\User\Models\User::class, function (Faker\Generator $faker) {
    return [
        'name'     => $faker->name,
        'email'    => $faker->email,
        'password' => bcrypt(str_random(10)),

// ...

Usage from Tests or anywhere else:


// creating 4 users
factory(User::class, 4)->create();

Usage with relationships:


$countries = Country::all();

// creating 3 rewards and attaching country relation to them
$rewards = factory(Reward::class, 3)->make()->each(function ($reward) use ($countries) {
    $reward->countries()->attach([$countries->random(1)->id, $countries->random(1)->id]);

Use make instance of create and pass any data any way, then save after establishing the relations.

Usage while overriding some values:


// creating single Offer and setting a user id
$offer = factory(Offer::class)->make();
$offer->user_id = $user->id;


// creating multiple Accounts
factory(Account::class, 3)->make()->each(function ($account) use ($user) {
    $account->user_id = $user->id;

For more information about the Models Factories read this.