Skip to content

stephanecoinon/object-factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Object Factory for Plain Old PHP Objects

Would you like to build plain PHP objects using Laravel 5 model factories?

BuiltByFactory trait makes it a breeze...

Step 1: Install this package

composer require stephanecoinon/object-factory

Step 2: define your plain model using the trait:

<?php

// /app/Popo.php

namespace App;

use StephaneCoinon\ObjectFactory\BuiltByFactory;

/**
 * This is a plain old PHP class, we do not extend Eloquent.
 */
class Popo
{
    use BuiltByFactory;

    /**
     * Objects built by model factory are constructed from an array of
     * attributes.
     *
     * Here's an example of implementation.
     *
     * @param  array  $attributes
     * @return static
     */
    public function __construct($attributes = [])
    {
        foreach ($attributes as $key => $value) {
            $this->$key = $value;
        }
    }
}

Step 3: define the factory:

<?php

// /database/factories/PopoFactory.php

use Faker\Generator as Faker;

$factory->define(App\Popo::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
    ];
});

Step 4: build your objects!

// A single instance
$object = factory(App\Popo::class)->make();

// Or a collection
$objects = factory(App\Popo::class, 3)->make();

About

Create plain PHP objects using Laravel model factories

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages