abstract class Factory (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected TModel> $model

The name of the factory's corresponding model.

protected int|null $count

The number of models that should be generated.

protected Collection $states

The state transformations that will be applied to the model.

protected Collection $has

The parent relationships that will be applied to the model.

protected Collection $for

The child relationships that will be applied to the model.

protected Collection $recycle

The model instances to always use when creating relationships.

protected Collection $afterMaking

The "after making" callbacks that will be applied to the model.

protected Collection $afterCreating

The "after creating" callbacks that will be applied to the model.

protected bool $expandRelationships

Whether relationships should not be automatically created.

protected string|null $connection

The name of the database connection that will be used to create the models.

protected Generator $faker

The current Faker instance.

static string $namespace

The default namespace where factories reside.

static protected $modelNameResolver

The default model name resolver.

static protected callable $factoryNameResolver

The factory name resolver.

Methods

$this|TWhenReturnType
when($value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

$this|TUnlessReturnType
unless($value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

mixed
forwardCallTo(mixed $object, string $method, array $parameters)

Forward a method call to the given object.

mixed
forwardDecoratedCallTo(mixed $object, string $method, array $parameters)

Forward a method call to the given object, returning $this if the forwarded call returned itself.

static void
throwBadMethodCallException(string $method)

Throw a bad method call exception for the given method.

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 void
flushMacros()

Flush the existing macros.

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

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Proxy dynamic factory methods onto their proper methods.

void
__construct(int|null $count = null, Collection|null $states = null, Collection|null $has = null, Collection|null $for = null, Collection|null $afterMaking = null, Collection|null $afterCreating = null, string|null $connection = null, Collection|null $recycle = null, bool $expandRelationships = true)

Create a new factory instance.

array<string,mixed>
definition()

Define the model's default state.

static Factory
new($attributes = [])

Get a new factory instance for the given attributes.

static Factory
times(int $count)

Get a new factory instance for the given number of models.

configure()

Configure the factory.

array<int|string,mixed>
raw($attributes = [], Model|null $parent = null)

Get the raw attributes generated by the factory.

TModel
createOne($attributes = [])

Create a single model and persist it to the database.

TModel
createOneQuietly($attributes = [])

Create a single model and persist it to the database without dispatching any model events.

TModel>
createMany(int|iterable|null $records = null)

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

TModel>
createManyQuietly(int|iterable|null $records = null)

Create a collection of models and persist them to the database without dispatching any model events.

TModel>|TModel
create($attributes = [], Model|null $parent = null)

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

TModel>|TModel
createQuietly($attributes = [], Model|null $parent = null)

Create a collection of models and persist them to the database without dispatching any model events.

lazy(array $attributes = [], Model|null $parent = null)

No description

void
store(Collection $results)

Set the connection name on the results and store them.

void
createChildren(Model $model)

Create the children for the given model.

TModel
makeOne($attributes = [])

Make a single instance of the model.

TModel>|TModel
make($attributes = [], Model|null $parent = null)

Create a collection of models.

makeInstance(Model|null $parent)

Make an instance of the model with the given attributes.

mixed
getExpandedAttributes(Model|null $parent)

Get a raw attributes array for the model.

array
getRawAttributes(Model|null $parent)

Get the raw attributes for the model as an array.

array
parentResolvers()

Create the parent relationship resolvers (as deferred Closures).

array
expandAttributes(array $definition)

Expand all attributes to their underlying values.

state($state)

Add a new state transformation to the model definition.

set(string|int $key, mixed $value)

Set a single model attribute.

sequence(mixed ...$sequence)

Add a new sequenced state transformation to the model definition.

forEachSequence(array ...$sequence)

Add a new sequenced state transformation to the model definition and update the pending creation count to the size of the sequence.

crossJoinSequence(array ...$sequence)

Add a new cross joined sequenced state transformation to the model definition.

has(Factory $factory, string|null $relationship = null)

Define a child relationship for the model.

string
guessRelationship(string $related)

Attempt to guess the relationship name for a "has" relationship.

hasAttached($factory, $pivot = [], $relationship = null)

No description

for(Factory|Model $factory, string|null $relationship = null)

Define a parent relationship for the model.

recycle(Model|Collection|array $model)

Provide model instances to use instead of any nested factory calls when creating relationships.

TClass|null
getRandomRecycledModel(TClass> $modelClassName)

Retrieve a random model of a given type from previously provided models to recycle.

afterMaking(Closure $callback)

Add a new "after making" callback to the model definition.

afterCreating(Closure $callback)

Add a new "after creating" callback to the model definition.

void
callAfterMaking(Collection $instances)

Call the "after making" callbacks for the given model instances.

void
callAfterCreating(Collection $instances, Model|null $parent = null)

Call the "after creating" callbacks for the given model instances.

count(int|null $count)

Specify how many models should be generated.

withoutParents()

Indicate that related parent models should not be created.

string
getConnectionName()

Get the name of the database connection that is used to generate models.

connection(string $connection)

Specify the database connection that should be used to generate models.

newInstance(array $arguments = [])

Create a new instance of the factory builder with the given mutated properties.

TModel
newModel(array $attributes = [])

Get a new model instance.

TModel>
modelName()

Get the name of the model that is generated by the factory.

static void
guessModelNamesUsing(callable $callback)

Specify the callback that should be invoked to guess model names based on factory names.

static void
useNamespace(string $namespace)

Specify the default namespace that contains the application's model factories.

static TClass>
factoryForModel(string $modelName)

Get a new factory instance for the given model name.

static void
guessFactoryNamesUsing(callable $callback)

Specify the callback that should be invoked to guess factory names based on dynamic relationship names.

Generator
withFaker()

Get a new Faker instance.

static 
resolveFactoryName(string $modelName)

No description

static string
appNamespace()

Get the application namespace for the application.

$this
trashed()

No description

Details

$this|TWhenReturnType when($value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

Parameters

$value
callable|null $callback
callable|null $default

Return Value

$this|TWhenReturnType

$this|TUnlessReturnType unless($value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

Parameters

$value
callable|null $callback
callable|null $default

Return Value

$this|TUnlessReturnType

protected mixed forwardCallTo(mixed $object, string $method, array $parameters)

Forward a method call to the given object.

Parameters

mixed $object
string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

protected mixed forwardDecoratedCallTo(mixed $object, string $method, array $parameters)

Forward a method call to the given object, returning $this if the forwarded call returned itself.

Parameters

mixed $object
string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

static protected void throwBadMethodCallException(string $method)

Throw a bad method call exception for the given method.

Parameters

string $method

Return Value

void

Exceptions

BadMethodCallException

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 void flushMacros()

Flush the existing macros.

Return Value

void

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)

Proxy dynamic factory methods onto their proper methods.

Parameters

string $method
array $parameters

Return Value

mixed

void __construct(int|null $count = null, Collection|null $states = null, Collection|null $has = null, Collection|null $for = null, Collection|null $afterMaking = null, Collection|null $afterCreating = null, string|null $connection = null, Collection|null $recycle = null, bool $expandRelationships = true)

Create a new factory instance.

Parameters

int|null $count
Collection|null $states
Collection|null $has
Collection|null $for
Collection|null $afterMaking
Collection|null $afterCreating
string|null $connection
Collection|null $recycle
bool $expandRelationships

Return Value

void

abstract array<string,mixed> definition()

Define the model's default state.

Return Value

array<string,mixed>

static Factory new($attributes = [])

Get a new factory instance for the given attributes.

Parameters

$attributes

Return Value

Factory

static Factory times(int $count)

Get a new factory instance for the given number of models.

Parameters

int $count

Return Value

Factory

Factory configure()

Configure the factory.

Return Value

Factory

array<int|string,mixed> raw($attributes = [], Model|null $parent = null)

Get the raw attributes generated by the factory.

Parameters

$attributes
Model|null $parent

Return Value

array<int|string,mixed>

TModel createOne($attributes = [])

Create a single model and persist it to the database.

Parameters

$attributes

Return Value

TModel

TModel createOneQuietly($attributes = [])

Create a single model and persist it to the database without dispatching any model events.

Parameters

$attributes

Return Value

TModel

TModel> createMany(int|iterable|null $records = null)

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

Parameters

int|iterable|null $records

Return Value

TModel>

TModel> createManyQuietly(int|iterable|null $records = null)

Create a collection of models and persist them to the database without dispatching any model events.

Parameters

int|iterable|null $records

Return Value

TModel>

TModel>|TModel create($attributes = [], Model|null $parent = null)

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

Parameters

$attributes
Model|null $parent

Return Value

TModel>|TModel

TModel>|TModel createQuietly($attributes = [], Model|null $parent = null)

Create a collection of models and persist them to the database without dispatching any model events.

Parameters

$attributes
Model|null $parent

Return Value

TModel>|TModel

lazy(array $attributes = [], Model|null $parent = null)

No description

Parameters

array $attributes
Model|null $parent

protected void store(Collection $results)

Set the connection name on the results and store them.

Parameters

Collection $results

Return Value

void

protected void createChildren(Model $model)

Create the children for the given model.

Parameters

Model $model

Return Value

void

TModel makeOne($attributes = [])

Make a single instance of the model.

Parameters

$attributes

Return Value

TModel

TModel>|TModel make($attributes = [], Model|null $parent = null)

Create a collection of models.

Parameters

$attributes
Model|null $parent

Return Value

TModel>|TModel

protected Model makeInstance(Model|null $parent)

Make an instance of the model with the given attributes.

Parameters

Model|null $parent

Return Value

Model

protected mixed getExpandedAttributes(Model|null $parent)

Get a raw attributes array for the model.

Parameters

Model|null $parent

Return Value

mixed

protected array getRawAttributes(Model|null $parent)

Get the raw attributes for the model as an array.

Parameters

Model|null $parent

Return Value

array

protected array parentResolvers()

Create the parent relationship resolvers (as deferred Closures).

Return Value

array

protected array expandAttributes(array $definition)

Expand all attributes to their underlying values.

Parameters

array $definition

Return Value

array

Factory state($state)

Add a new state transformation to the model definition.

Parameters

$state

Return Value

Factory

Factory set(string|int $key, mixed $value)

Set a single model attribute.

Parameters

string|int $key
mixed $value

Return Value

Factory

Factory sequence(mixed ...$sequence)

Add a new sequenced state transformation to the model definition.

Parameters

mixed ...$sequence

Return Value

Factory

Factory forEachSequence(array ...$sequence)

Add a new sequenced state transformation to the model definition and update the pending creation count to the size of the sequence.

Parameters

array ...$sequence

Return Value

Factory

Factory crossJoinSequence(array ...$sequence)

Add a new cross joined sequenced state transformation to the model definition.

Parameters

array ...$sequence

Return Value

Factory

Factory has(Factory $factory, string|null $relationship = null)

Define a child relationship for the model.

Parameters

Factory $factory
string|null $relationship

Return Value

Factory

protected string guessRelationship(string $related)

Attempt to guess the relationship name for a "has" relationship.

Parameters

string $related

Return Value

string

hasAttached($factory, $pivot = [], $relationship = null)

No description

Parameters

$factory
$pivot
$relationship

Factory for(Factory|Model $factory, string|null $relationship = null)

Define a parent relationship for the model.

Parameters

Factory|Model $factory
string|null $relationship

Return Value

Factory

Factory recycle(Model|Collection|array $model)

Provide model instances to use instead of any nested factory calls when creating relationships.

Parameters

Model|Collection|array $model

Return Value

Factory

TClass|null getRandomRecycledModel(TClass> $modelClassName)

Retrieve a random model of a given type from previously provided models to recycle.

Parameters

TClass> $modelClassName

Return Value

TClass|null

Factory afterMaking(Closure $callback)

Add a new "after making" callback to the model definition.

Parameters

Closure $callback

Return Value

Factory

Factory afterCreating(Closure $callback)

Add a new "after creating" callback to the model definition.

Parameters

Closure $callback

Return Value

Factory

protected void callAfterMaking(Collection $instances)

Call the "after making" callbacks for the given model instances.

Parameters

Collection $instances

Return Value

void

protected void callAfterCreating(Collection $instances, Model|null $parent = null)

Call the "after creating" callbacks for the given model instances.

Parameters

Collection $instances
Model|null $parent

Return Value

void

Factory count(int|null $count)

Specify how many models should be generated.

Parameters

int|null $count

Return Value

Factory

Factory withoutParents()

Indicate that related parent models should not be created.

Return Value

Factory

string getConnectionName()

Get the name of the database connection that is used to generate models.

Return Value

string

Factory connection(string $connection)

Specify the database connection that should be used to generate models.

Parameters

string $connection

Return Value

Factory

protected Factory newInstance(array $arguments = [])

Create a new instance of the factory builder with the given mutated properties.

Parameters

array $arguments

Return Value

Factory

TModel newModel(array $attributes = [])

Get a new model instance.

Parameters

array $attributes

Return Value

TModel

TModel> modelName()

Get the name of the model that is generated by the factory.

Return Value

TModel>

static void guessModelNamesUsing(callable $callback)

Specify the callback that should be invoked to guess model names based on factory names.

Parameters

callable $callback

Return Value

void

static void useNamespace(string $namespace)

Specify the default namespace that contains the application's model factories.

Parameters

string $namespace

Return Value

void

static TClass> factoryForModel(string $modelName)

Get a new factory instance for the given model name.

Parameters

string $modelName

Return Value

TClass>

static void guessFactoryNamesUsing(callable $callback)

Specify the callback that should be invoked to guess factory names based on dynamic relationship names.

Parameters

callable $callback

Return Value

void

protected Generator withFaker()

Get a new Faker instance.

Return Value

Generator

static resolveFactoryName(string $modelName)

No description

Parameters

string $modelName

static protected string appNamespace()

Get the application namespace for the application.

Return Value

string

$this trashed()

No description

Return Value

$this