abstract class Factory (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected $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 $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 string $connection

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

protected Generator $faker

The current Faker instance.

static protected string $namespace

The default namespace where factories reside.

static protected callable $modelNameResolver

The default model name resolver.

static protected callable $factoryNameResolver

The factory name resolver.

Methods

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

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

$this|TUnlessReturnType
unless(bool $value, callable $callback = null, callable $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)

Create a new factory instance.

array<string,mixed>
definition()

Define the model's default state.

static 
new($attributes = [])

No description

static Factory
times(int $count)

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

$this
configure()

Configure the factory.

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

Get the raw attributes generated by the factory.

Model|TModel
createOne(array<string,mixed> $attributes = [])

Create a single model and persist it to the database.

Model|TModel
createOneQuietly(array<string,mixed> $attributes = [])

Create a single model and persist it to the database.

Model|TModel>
createMany(iterable $records)

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

Model|TModel>
createManyQuietly(iterable $records)

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

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

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

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

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

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.

makeOne($attributes = [])

No description

Model|TModel>|Model|TModel
make(array<string,mixed> $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)

No description

sequence(array ...$sequence)

Add a new 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.

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.

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.

Model|TModel
newModel(array $attributes = [])

Get a new model instance.

modelName()

No description

static 
guessModelNamesUsing(callable $callback)

No description

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 
guessFactoryNamesUsing(callable $callback)

No description

Generator
withFaker()

Get a new Faker instance.

static Factory>
resolveFactoryName(string $modelName)

Get the factory name for the given model name.

static string
appNamespace()

Get the application namespace for the application.

Details

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

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

Parameters

bool $value
callable $callback
callable $default

Return Value

$this|TWhenReturnType

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

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

Parameters

bool $value
callable $callback
callable $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)

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

Return Value

void

abstract array<string,mixed> definition()

Define the model's default state.

Return Value

array<string,mixed>

static new($attributes = [])

No description

Parameters

$attributes

static Factory times(int $count)

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

Parameters

int $count

Return Value

Factory

$this configure()

Configure the factory.

Return Value

$this

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

Get the raw attributes generated by the factory.

Parameters

array<string,mixed> $attributes
Model|null $parent

Return Value

array<int|string,mixed>

Model|TModel createOne(array<string,mixed> $attributes = [])

Create a single model and persist it to the database.

Parameters

array<string,mixed> $attributes

Return Value

Model|TModel

Model|TModel createOneQuietly(array<string,mixed> $attributes = [])

Create a single model and persist it to the database.

Parameters

array<string,mixed> $attributes

Return Value

Model|TModel

Model|TModel> createMany(iterable $records)

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

Parameters

iterable $records

Return Value

Model|TModel>

Model|TModel> createManyQuietly(iterable $records)

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

Parameters

iterable $records

Return Value

Model|TModel>

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

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

Parameters

array<string,mixed> $attributes
Model|null $parent

Return Value

Model|TModel>|Model|TModel

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

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

Parameters

array<string,mixed> $attributes
Model|null $parent

Return Value

Model|TModel>|Model|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

makeOne($attributes = [])

No description

Parameters

$attributes

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

Create a collection of models.

Parameters

array<string,mixed> $attributes
Model|null $parent

Return Value

Model|TModel>|Model|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

state($state)

No description

Parameters

$state

Factory sequence(array ...$sequence)

Add a new 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 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 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

Model|TModel newModel(array $attributes = [])

Get a new model instance.

Parameters

array $attributes

Return Value

Model|TModel

modelName()

No description

static guessModelNamesUsing(callable $callback)

No description

Parameters

callable $callback

static void useNamespace(string $namespace)

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

Parameters

string $namespace

Return Value

void

static Factory factoryForModel(string $modelName)

Get a new factory instance for the given model name.

Parameters

string $modelName

Return Value

Factory

static guessFactoryNamesUsing(callable $callback)

No description

Parameters

callable $callback

protected Generator withFaker()

Get a new Faker instance.

Return Value

Generator

static Factory> resolveFactoryName(string $modelName)

Get the factory name for the given model name.

Parameters

string $modelName

Return Value

Factory>

static protected string appNamespace()

Get the application namespace for the application.

Return Value

string