abstract class Factory

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected null|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 array $excludeRelationships

The relationships that should not be automatically created.

protected UnitEnum|string|null $connection

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

protected Generator|null $faker

The current Faker instance.

static string $namespace

The default namespace where factories reside.

static protected TModel>> $modelNameResolvers

The default model name resolvers.

static protected null|callable $factoryNameResolver

The factory name resolver.

static protected bool $expandRelationshipsByDefault

Whether to expand relationships by default.

static protected TModel>|false> $cachedModelAttributes

The cached model class names resolved from attributes.

Methods

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

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

mixed
unless(mixed $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 never
throwBadMethodCallException(string $method)

Throw a bad method call exception for the given method.

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

Register a custom macro.

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

Mix another object into the class.

static bool
hasMacro(string $name)

Check 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.

__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, UnitEnum|string|null $connection = null, Collection|null $recycle = null, bool|null $expandRelationships = null, array $excludeRelationships = [], string|null $model = null)

Create a new factory instance.

array
definition()

Define the model's default state.

static Factory
new(callable|array $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
raw(callable|array $attributes = [], Model|null $parent = null)

Get the raw attributes generated by the factory.

createOne(callable|array $attributes = [])

Create a single model and persist it to the database.

createOneQuietly(callable|array $attributes = [])

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

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

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

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

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

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

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

createQuietly(callable|array $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)

Create a callback that persists a model in the database when invoked.

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.

makeOne(callable|array $attributes = [])

Make a single instance of the model.

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

Create a collection of models.

makeMany(iterable|int|null $records = null)

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

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

Insert the model records in bulk. No model events are emitted.

makeInstance(Model|null $parent)

Make an instance of the model with the given attributes.

array
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(callable|array $state)

Add a new state transformation to the model definition.

prependState(callable|array $state)

Prepend 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|Collection|Model|array $factory, callable|array $pivot = [], string|null $relationship = null)

Define an attached relationship for the model.

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.

Model|null
getRandomRecycledModel(string $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.

withoutAfterMaking()

Remove the "after making" callbacks from the factory.

withoutAfterCreating()

Remove the "after creating" callbacks from the factory.

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(array $parents = [])

Indicate that related parent models should not be created.

string|null
getConnectionName()

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

connection(UnitEnum|string|null $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.

useModel(string $model)

Specify the model that should be generated by the factory.

newModel(array $attributes = [])

Get a new model instance.

string
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 Factory
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.

static void
expandRelationshipsByDefault()

Specify that relationships should create parent relationships by default.

static void
dontExpandRelationshipsByDefault()

Specify that relationships should not create parent relationships by default.

Generator|null
withFaker()

Get a new Faker instance.

static string
resolveFactoryName(string $modelName)

Get the factory name for the given model name.

static string
appNamespace()

Get the application namespace for the application.

static void
flushState()

Flush all static state.

$this
trashed()

No description

Details

in Conditionable at line 23
mixed when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

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

Parameters

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

Return Value

mixed

in Conditionable at line 56
mixed unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

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

Parameters

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

Return Value

mixed

in ForwardsCalls at line 22
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

in ForwardsCalls at line 52
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

in ForwardsCalls at line 66
static protected never throwBadMethodCallException(string $method)

Throw a bad method call exception for the given method.

Parameters

string $method

Return Value

never

Exceptions

BadMethodCallException

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 1028
mixed __call(string $method, array $parameters)

Proxy dynamic factory methods onto their proper methods.

Parameters

string $method
array $parameters

Return Value

mixed

at line 140
__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, UnitEnum|string|null $connection = null, Collection|null $recycle = null, bool|null $expandRelationships = null, array $excludeRelationships = [], string|null $model = null)

Create a new factory instance.

Parameters

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

at line 172
abstract array definition()

Define the model's default state.

Return Value

array

at line 179
static Factory new(callable|array $attributes = [])

Get a new factory instance for the given attributes.

Parameters

callable|array $attributes

Return Value

Factory

at line 187
static Factory times(int $count)

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

Parameters

int $count

Return Value

Factory

at line 195
Factory configure()

Configure the factory.

Return Value

Factory

at line 206
array raw(callable|array $attributes = [], Model|null $parent = null)

Get the raw attributes generated by the factory.

Parameters

callable|array $attributes
Model|null $parent

Return Value

array

at line 223
Model createOne(callable|array $attributes = [])

Create a single model and persist it to the database.

Parameters

callable|array $attributes

Return Value

Model

at line 234
Model createOneQuietly(callable|array $attributes = [])

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

Parameters

callable|array $attributes

Return Value

Model

at line 245
Collection createMany(int|iterable|null $records = null)

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

Parameters

int|iterable|null $records

Return Value

Collection

at line 269
Collection 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

Collection

at line 280
Collection|Model create(callable|array $attributes = [], Model|null $parent = null)

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

Parameters

callable|array $attributes
Model|null $parent

Return Value

Collection|Model

at line 307
Collection|Model createQuietly(callable|array $attributes = [], Model|null $parent = null)

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

Parameters

callable|array $attributes
Model|null $parent

Return Value

Collection|Model

at line 318
Closure lazy(array $attributes = [], Model|null $parent = null)

Create a callback that persists a model in the database when invoked.

Parameters

array $attributes
Model|null $parent

Return Value

Closure

at line 328
protected void store(Collection $results)

Set the connection name on the results and store them.

Parameters

Collection $results

Return Value

void

at line 350
protected void createChildren(Model $model)

Create the children for the given model.

Parameters

Model $model

Return Value

void

at line 365
Model makeOne(callable|array $attributes = [])

Make a single instance of the model.

Parameters

callable|array $attributes

Return Value

Model

at line 376
Collection|Model make(callable|array $attributes = [], Model|null $parent = null)

Create a collection of models.

Parameters

callable|array $attributes
Model|null $parent

Return Value

Collection|Model

at line 416
Collection makeMany(iterable|int|null $records = null)

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

Parameters

iterable|int|null $records

Return Value

Collection

at line 439
void insert(array $attributes = [], Model|null $parent = null)

Insert the model records in bulk. No model events are emitted.

Parameters

array $attributes
Model|null $parent

Return Value

void

at line 468
protected Model makeInstance(Model|null $parent)

Make an instance of the model with the given attributes.

Parameters

Model|null $parent

Return Value

Model

at line 482
protected array getExpandedAttributes(Model|null $parent)

Get a raw attributes array for the model.

Parameters

Model|null $parent

Return Value

array

at line 490
protected array getRawAttributes(Model|null $parent)

Get the raw attributes for the model as an array.

Parameters

Model|null $parent

Return Value

array

at line 508
protected array parentResolvers()

Create the parent relationship resolvers (as deferred Closures).

Return Value

array

at line 519
protected array expandAttributes(array $definition)

Expand all attributes to their underlying values.

Parameters

array $definition

Return Value

array

at line 556
Factory state(callable|array $state)

Add a new state transformation to the model definition.

Parameters

callable|array $state

Return Value

Factory

at line 570
Factory prependState(callable|array $state)

Prepend a new state transformation to the model definition.

Parameters

callable|array $state

Return Value

Factory

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

Set a single model attribute.

Parameters

string|int $key
mixed $value

Return Value

Factory

at line 590
Factory sequence(mixed ...$sequence)

Add a new sequenced state transformation to the model definition.

Parameters

mixed ...$sequence

Return Value

Factory

at line 598
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

at line 606
Factory crossJoinSequence(array ...$sequence)

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

Parameters

array ...$sequence

Return Value

Factory

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

Define a child relationship for the model.

Parameters

Factory $factory
string|null $relationship

Return Value

Factory

at line 627
protected string guessRelationship(string $related)

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

Parameters

string $related

Return Value

string

at line 639
Factory hasAttached(Factory|Collection|Model|array $factory, callable|array $pivot = [], string|null $relationship = null)

Define an attached relationship for the model.

Parameters

Factory|Collection|Model|array $factory
callable|array $pivot
string|null $relationship

Return Value

Factory

at line 665
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

at line 678
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

at line 699
Model|null getRandomRecycledModel(string $modelClassName)

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

Parameters

string $modelClassName

Return Value

Model|null

at line 709
Factory afterMaking(Closure $callback)

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

Parameters

Closure $callback

Return Value

Factory

at line 719
Factory afterCreating(Closure $callback)

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

Parameters

Closure $callback

Return Value

Factory

at line 727
Factory withoutAfterMaking()

Remove the "after making" callbacks from the factory.

Return Value

Factory

at line 735
Factory withoutAfterCreating()

Remove the "after creating" callbacks from the factory.

Return Value

Factory

at line 743
protected void callAfterMaking(Collection $instances)

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

Parameters

Collection $instances

Return Value

void

at line 755
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

at line 767
Factory count(int|null $count)

Specify how many models should be generated.

Parameters

int|null $count

Return Value

Factory

at line 777
Factory withoutParents(array $parents = [])

Indicate that related parent models should not be created.

Parameters

array $parents

Return Value

Factory

at line 785
string|null getConnectionName()

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

Return Value

string|null

at line 795
Factory connection(UnitEnum|string|null $connection)

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

Parameters

UnitEnum|string|null $connection

Return Value

Factory

at line 803
protected Factory newInstance(array $arguments = [])

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

Parameters

array $arguments

Return Value

Factory

at line 828
Factory useModel(string $model)

Specify the model that should be generated by the factory.

An explicit UseModel attribute on the factory takes precedence.

Parameters

string $model

Return Value

Factory

at line 839
Model newModel(array $attributes = [])

Get a new model instance.

Parameters

array $attributes

Return Value

Model

at line 851
string modelName()

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

Return Value

string

at line 896
static void guessModelNamesUsing(callable $callback)

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

Boot or tests only. The callback persists in a static property for the worker lifetime and runs on every model-name guess across all coroutines.

Parameters

callable $callback

Return Value

void

at line 907
static void useNamespace(string $namespace)

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

Boot or tests only. The namespace persists in a static property for the worker lifetime and applies to every subsequent factory-name lookup.

Parameters

string $namespace

Return Value

void

at line 920
static Factory factoryForModel(string $modelName)

Get a new factory instance for the given model name.

Parameters

string $modelName

Return Value

Factory

at line 936
static void guessFactoryNamesUsing(callable $callback)

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

Boot or tests only. The callback persists in a static property for the worker lifetime and runs on every factory-name guess across all coroutines.

Parameters

callable $callback

Return Value

void

at line 947
static void expandRelationshipsByDefault()

Specify that relationships should create parent relationships by default.

Boot or tests only. The flag persists in a static property for the worker lifetime and applies to every factory build across all coroutines.

Return Value

void

at line 958
static void dontExpandRelationshipsByDefault()

Specify that relationships should not create parent relationships by default.

Boot or tests only. The flag persists in a static property for the worker lifetime and applies to every factory build across all coroutines.

Return Value

void

at line 966
protected Generator|null withFaker()

Get a new Faker instance.

Return Value

Generator|null

at line 983
static string resolveFactoryName(string $modelName)

Get the factory name for the given model name.

Parameters

string $modelName

Return Value

string

at line 1001
static protected string appNamespace()

Get the application namespace for the application.

Return Value

string

at line 1015
static void flushState()

Flush all static state.

Return Value

void

at line 33
$this trashed()

No description

Return Value

$this