PHP code example of dbt / model-factory

1. Go to this page and download the library: Download dbt/model-factory library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dbt / model-factory example snippets


use Dbt\ModelFactory\ModelFactory;

class MyModelFactory extends ModelFactory
{
    protected $model = MyModel::class;

    /**
     * This is the main factory definition.
     * @return array
     */
    public function definition (): array
    {
        return [
            'my_string_column' => $this->faker->name,
        ];
    }

    /**
     * This will happen after the model is created.
     * @return void
     */
    public function after (MyModel $model): void
    {
        // Do some stuff to the model.
    }

    /**
     * This is a factory state.
     * @return array
     */
    public function myState (): array
    {
        return [
            'my_int_column' => rand(1, 10),
        ];
    }

     /**
      * This will happen after the model is created in the given state.
      * @return void
      */
     public function afterMyState (MyModel $model): void
     {
         // Do some stuff to the model.
     }
}

'classes' => [
    // ...
    MyModelFactory::class,
];

// Factory without state.
$model = factory(MyModel::class)->create();

// Factory with state.
$modelWithState = factory(MyModel::class)->states('myState')->create();

$model = Create::a(new MyModel, new Count(10), new States('myState'), new Overrides(['column' => 'value'])); 

// Create a model with defaults.
$model = Create::a(new MyModel);

// The following are all equivalent:
Create::a(new MyModel, new Count(...), new States(...), new Overrides(...));
Create::a(new MyModel, new States(...), new Count(...), new Overrides(...));
Create::a(new MyModel, new States(...), new Overrides(...), new Count(...));

// Etc.