class FactoryBuilder (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected array $definitions

The model definitions in the container.

protected string $class

The model being built.

protected string $connection

The database connection on which the model instance should be persisted.

protected array $states

The model states.

protected array $afterMaking

The model after making callbacks.

protected array $afterCreating

The model after creating callbacks.

protected array $activeStates

The states to apply.

protected Generator $faker

The Faker instance for the builder.

protected int|null $amount

The number of models to build.

Methods

static void
macro(string $name, object|callable $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Checks if macro is registered.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

void
__construct(string $class, array $definitions, array $states, array $afterMaking, array $afterCreating, Generator $faker)

Create an new builder instance.

$this
times(int $amount)

Set the amount of models you wish to create / make.

$this
state(string $state)

Set the state to be applied to the model.

$this
states(array|mixed $states)

Set the states to be applied to the model.

$this
connection(string $name)

Set the database connection on which the model instance should be persisted.

lazy(array $attributes = [])

Create a model and persist it in the database if requested.

create(array $attributes = [])

Create a collection of models and persist them to the database.

Collection|mixed
createMany(iterable $records)

Create a collection of models and persist them to the database.

void
store(Collection $results)

Set the connection name on the results and store them.

make(array $attributes = [])

Create a collection of models.

mixed
raw(array $attributes = [])

Create an array of raw attribute arrays.

mixed
getRawAttributes(array $attributes = [])

Get a raw attributes array for the model.

makeInstance(array $attributes = [])

Make an instance of the model with the given attributes.

array
applyStates(array $definition, array $attributes = [])

Apply the active states to the model definition array.

array
stateAttributes(string $state, array $attributes)

Get the state attributes.

array
expandAttributes(array $attributes)

Expand all attributes to their underlying values.

void
callAfterMaking(Collection $models)

Run after making callbacks on a collection of models.

void
callAfterCreating(Collection $models)

Run after creating callbacks on a collection of models.

void
callAfter(array $afterCallbacks, Collection $models)

Call after callbacks for each model and state.

void
callAfterCallbacks(array $afterCallbacks, Model $model, string $state)

Call after callbacks for each model and state.

bool
stateHasAfterCallback(string $state)

Determine if the given state has an "after" callback.

Details

static void macro(string $name, object|callable $macro)

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

void __construct(string $class, array $definitions, array $states, array $afterMaking, array $afterCreating, Generator $faker)

Create an new builder instance.

Parameters

string $class
array $definitions
array $states
array $afterMaking
array $afterCreating
Generator $faker

Return Value

void

$this times(int $amount)

Set the amount of models you wish to create / make.

Parameters

int $amount

Return Value

$this

$this state(string $state)

Set the state to be applied to the model.

Parameters

string $state

Return Value

$this

$this states(array|mixed $states)

Set the states to be applied to the model.

Parameters

array|mixed $states

Return Value

$this

$this connection(string $name)

Set the database connection on which the model instance should be persisted.

Parameters

string $name

Return Value

$this

Closure lazy(array $attributes = [])

Create a model and persist it in the database if requested.

Parameters

array $attributes

Return Value

Closure

Collection|Model|mixed create(array $attributes = [])

Create a collection of models and persist them to the database.

Parameters

array $attributes

Return Value

Collection|Model|mixed

Collection|mixed createMany(iterable $records)

Create a collection of models and persist them to the database.

Parameters

iterable $records

Return Value

Collection|mixed

protected void store(Collection $results)

Set the connection name on the results and store them.

Parameters

Collection $results

Return Value

void

Collection|Model|mixed make(array $attributes = [])

Create a collection of models.

Parameters

array $attributes

Return Value

Collection|Model|mixed

mixed raw(array $attributes = [])

Create an array of raw attribute arrays.

Parameters

array $attributes

Return Value

mixed

protected mixed getRawAttributes(array $attributes = [])

Get a raw attributes array for the model.

Parameters

array $attributes

Return Value

mixed

Exceptions

InvalidArgumentException

protected Model makeInstance(array $attributes = [])

Make an instance of the model with the given attributes.

Parameters

array $attributes

Return Value

Model

protected array applyStates(array $definition, array $attributes = [])

Apply the active states to the model definition array.

Parameters

array $definition
array $attributes

Return Value

array

Exceptions

InvalidArgumentException

protected array stateAttributes(string $state, array $attributes)

Get the state attributes.

Parameters

string $state
array $attributes

Return Value

array

protected array expandAttributes(array $attributes)

Expand all attributes to their underlying values.

Parameters

array $attributes

Return Value

array

void callAfterMaking(Collection $models)

Run after making callbacks on a collection of models.

Parameters

Collection $models

Return Value

void

void callAfterCreating(Collection $models)

Run after creating callbacks on a collection of models.

Parameters

Collection $models

Return Value

void

protected void callAfter(array $afterCallbacks, Collection $models)

Call after callbacks for each model and state.

Parameters

array $afterCallbacks
Collection $models

Return Value

void

protected void callAfterCallbacks(array $afterCallbacks, Model $model, string $state)

Call after callbacks for each model and state.

Parameters

array $afterCallbacks
Model $model
string $state

Return Value

void

protected bool stateHasAfterCallback(string $state)

Determine if the given state has an "after" callback.

Parameters

string $state

Return Value

bool