Factory
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
Apply the callback if the given "value" is (or resolves to) truthy.
Apply the callback if the given "value" is (or resolves to) falsy.
Forward a method call to the given object.
Forward a method call to the given object, returning $this if the forwarded call returned itself.
Throw a bad method call exception for the given method.
Mix another object into the class.
Dynamically handle calls to the class.
Proxy dynamic factory methods onto their proper methods.
Create a new factory instance.
Define the model's default state.
Get a new factory instance for the given attributes.
Create a single model and persist it to the database.
Create a single model and persist it to the database without dispatching any model events.
Create a collection of models and persist them to the database.
Create a collection of models and persist them to the database without dispatching any model events.
Create a collection of models and persist them to the database without dispatching any model events.
Create a collection of models.
Create the parent relationship resolvers (as deferred Closures).
Expand all attributes to their underlying values.
Prepend a new state transformation to the model definition.
Add a new sequenced state transformation to the model definition.
Add a new sequenced state transformation to the model definition and update the pending creation count to the size of the sequence.
Add a new cross joined sequenced state transformation to the model definition.
Attempt to guess the relationship name for a "has" relationship.
Define an attached relationship for the model.
Provide model instances to use instead of any nested factory calls when creating relationships.
Retrieve a random model of a given type from previously provided models to recycle.
Remove the "after making" callbacks from the factory.
Remove the "after creating" callbacks from the factory.
Call the "after making" callbacks for the given model instances.
Call the "after creating" callbacks for the given model instances.
Indicate that related parent models should not be created.
Get the name of the database connection that is used to generate models.
Specify the database connection that should be used to generate models.
Create a new instance of the factory builder with the given mutated properties.
Get the name of the model that is generated by the factory.
Specify the callback that should be invoked to guess model names based on factory names.
Specify the default namespace that contains the application's model factories.
Get a new factory instance for the given model name.
Specify the callback that should be invoked to guess factory names based on dynamic relationship names.
Specify that relationships should create parent relationships by default.
Specify that relationships should not create parent relationships by default.
Get a new Faker instance.
Get the factory name for the given model name.
Get the application namespace for the application.
Flush all static state.
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.
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.
in
ForwardsCalls at line 22
protected mixed
forwardCallTo(mixed $object, string $method, array $parameters)
Forward a method call to the given object.
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.
in
ForwardsCalls at line 66
static protected never
throwBadMethodCallException(string $method)
Throw a bad method call exception for the given method.
in
Macroable at line 30
static void
macro(string $name, callable|object $macro)
Register a custom macro.
Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.
in
Macroable at line 44
static void
mixin(object $mixin, bool $replace = true)
Mix another object into the class.
Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.
in
Macroable at line 61
static bool
hasMacro(string $name)
Check if macro is registered.
in
Macroable at line 72
static void
flushMacros()
Flush the existing macros.
Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.
in
Macroable at line 83
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
at line 1085
mixed
__call(string $method, array $parameters)
Proxy dynamic factory methods onto their proper methods.
at line 141
__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.
at line 173
abstract array
definition()
Define the model's default state.
at line 180
static Factory
new(callable|array $attributes = [])
Get a new factory instance for the given attributes.
at line 188
static Factory
times(int $count)
Get a new factory instance for the given number of models.
at line 196
Factory
configure()
Configure the factory.
at line 207
array
raw(callable|array $attributes = [], Model|null $parent = null)
Get the raw attributes generated by the factory.
at line 228
Model
createOne(callable|array $attributes = [])
Create a single model and persist it to the database.
at line 239
Model
createOneQuietly(callable|array $attributes = [])
Create a single model and persist it to the database without dispatching any model events.
at line 250
Collection
createMany(int|iterable|null $records = null)
Create a collection of models and persist them to the database.
at line 280
Collection
createManyQuietly(int|iterable|null $records = null)
Create a collection of models and persist them to the database without dispatching any model events.
at line 291
Collection|Model
create(callable|array $attributes = [], Model|null $parent = null)
Create a collection of models and persist them to the database.
at line 318
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.
at line 329
Closure
lazy(array $attributes = [], Model|null $parent = null)
Create a callback that persists a model in the database when invoked.
at line 339
protected void
store(Collection $results)
Set the connection name on the results and store them.
at line 363
protected void
createChildren(Model $model)
Create the children for the given model.
at line 378
Model
makeOne(callable|array $attributes = [])
Make a single instance of the model.
at line 389
Collection|Model
make(callable|array $attributes = [], Model|null $parent = null)
Create a collection of models.
at line 423
Collection
makeMany(iterable|int|null $records = null)
Create a collection of models.
at line 452
void
insert(array $attributes = [], Model|null $parent = null)
Insert the model records in bulk. No model events are emitted.
at line 498
protected Model
makeInstance(Model|null $parent)
Make an instance of the model with the given attributes.
at line 512
protected array
getExpandedAttributes(Model|null $parent)
Get a raw attributes array for the model.
at line 520
protected array
getRawAttributes(Model|null $parent)
Get the raw attributes for the model as an array.
at line 538
protected array
parentResolvers()
Create the parent relationship resolvers (as deferred Closures).
at line 549
protected array
expandAttributes(array $definition)
Expand all attributes to their underlying values.
at line 586
Factory
state(callable|array $state)
Add a new state transformation to the model definition.
at line 600
Factory
prependState(callable|array $state)
Prepend a new state transformation to the model definition.
at line 612
Factory
set(string|int $key, mixed $value)
Set a single model attribute.
at line 620
Factory
sequence(mixed ...$sequence)
Add a new sequenced state transformation to the model definition.
at line 628
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.
at line 636
Factory
crossJoinSequence(array ...$sequence)
Add a new cross joined sequenced state transformation to the model definition.
at line 644
Factory
has(Factory $factory, string|null $relationship = null)
Define a child relationship for the model.
at line 663
protected string
guessRelationship(string $related)
Attempt to guess the relationship name for a "has" relationship.
at line 675
Factory
hasAttached(Factory|Collection|Model|array $factory, callable|array $pivot = [], string|null $relationship = null)
Define an attached relationship for the model.
at line 705
Factory
for(Factory|Model $factory, string|null $relationship = null)
Define a parent relationship for the model.
at line 724
Factory
recycle(Model|Collection|array $model)
Provide model instances to use instead of any nested factory calls when creating relationships.
at line 752
Model|null
getRandomRecycledModel(string $modelClassName)
Retrieve a random model of a given type from previously provided models to recycle.
at line 762
Factory
afterMaking(Closure $callback)
Add a new "after making" callback to the model definition.
at line 772
Factory
afterCreating(Closure $callback)
Add a new "after creating" callback to the model definition.
at line 780
Factory
withoutAfterMaking()
Remove the "after making" callbacks from the factory.
at line 788
Factory
withoutAfterCreating()
Remove the "after creating" callbacks from the factory.
at line 796
protected void
callAfterMaking(Collection $instances)
Call the "after making" callbacks for the given model instances.
at line 808
protected void
callAfterCreating(Collection $instances, Model|null $parent = null)
Call the "after creating" callbacks for the given model instances.
at line 820
Factory
count(int|null $count)
Specify how many models should be generated.
at line 830
Factory
withoutParents(array $parents = [])
Indicate that related parent models should not be created.
at line 838
string|null
getConnectionName()
Get the name of the database connection that is used to generate models.
at line 848
Factory
connection(UnitEnum|string|null $connection)
Specify the database connection that should be used to generate models.
at line 856
protected Factory
newInstance(array $arguments = [])
Create a new instance of the factory builder with the given mutated properties.
at line 881
Factory
useModel(string $model)
Specify the model that should be generated by the factory.
An explicit UseModel attribute on the factory takes precedence.
at line 892
Model
newModel(array $attributes = [])
Get a new model instance.
at line 904
string
modelName()
Get the name of the model that is generated by the factory.
at line 949
static void
guessModelNamesUsing(callable $callback)
Specify the callback that should be invoked to guess model names based on factory names.
The callback persists in a static property for the worker lifetime and runs on every model-name guess across all coroutines.
at line 961
static void
useNamespace(string $namespace)
Specify the default namespace that contains the application's model factories.
The namespace persists in a static property for the worker lifetime and applies to every subsequent factory-name lookup.
at line 975
static Factory
factoryForModel(string $modelName)
Get a new factory instance for the given model name.
at line 990
static void
guessFactoryNamesUsing(callable $callback)
Specify the callback that should be invoked to guess factory names based on dynamic relationship names.
The callback persists in a static property for the worker lifetime and runs on every factory-name guess across all coroutines.
at line 1002
static void
expandRelationshipsByDefault()
Specify that relationships should create parent relationships by default.
The flag persists in a static property for the worker lifetime and applies to every factory build across all coroutines.
at line 1014
static void
dontExpandRelationshipsByDefault()
Specify that relationships should not create parent relationships by default.
The flag persists in a static property for the worker lifetime and applies to every factory build across all coroutines.
at line 1023
protected Generator|null
withFaker()
Get a new Faker instance.
at line 1040
static string
resolveFactoryName(string $modelName)
Get the factory name for the given model name.
at line 1058
static protected string
appNamespace()
Get the application namespace for the application.
at line 1072
static void
flushState()
Flush all static state.
at line 34
$this
trashed()
No description