Factory
Uses
Properties
$model string|null
The name of the factory's corresponding model.
$count int|null
The number of models that should be generated.
$states Collection
The state transformations that will be applied to the model.
$afterMaking Collection
The "after making" callbacks that will be applied to the model.
$afterCreating Collection
The "after creating" callbacks that will be applied to the model.
$connection string
The name of the database connection that will be used to create the models.
$faker Faker\Generator
The current Faker instance.
$namespace string
The default namespace where factories reside.
$modelNameResolver callable
The default model name resolver.
$factoryNameResolver callable
The factory name resolver.
Methods
__construct ( $count , Collection $states , Collection $has , Collection $for , Collection $afterMaking , Collection $afterCreating , $connection ) : void
Create a new factory instance.
Parameters
- $count int|null
- $states Collection |null
- $has Collection |null
- $for Collection |null
- $afterMaking Collection |null
- $afterCreating Collection |null
- $connection string|null
definition ( ) : array
Define the model's default state.
new ( $attributes ) : static
Get a new factory instance for the given attributes.
Parameters
- $attributes callable|array
times ( int $count ) : static
Get a new factory instance for the given number of models.
Parameters
- $count int
configure ( ) : $this
Configure the factory.
raw ( $attributes , Model $parent ) : array
Get the raw attributes generated by the factory.
Parameters
- $attributes array
- $parent Model |null
createOne ( $attributes ) : Model
Create a single model and persist it to the database.
Parameters
- $attributes array
createOneQuietly ( $attributes ) : Model
Create a single model and persist it to the database.
Parameters
- $attributes array
createMany ( iterable $records ) : Collection
Create a collection of models and persist them to the database.
Parameters
- $records iterable
createManyQuietly ( iterable $records ) : Collection
Create a collection of models and persist them to the database.
Parameters
- $records iterable
create ( $attributes , Model $parent ) : Collection |Model
Create a collection of models and persist them to the database.
Parameters
- $attributes array
- $parent Model |null
createQuietly ( $attributes , Model $parent ) : Collection |Model
Create a collection of models and persist them to the database.
Parameters
- $attributes array
- $parent Model |null
lazy ( array $attributes , Model $parent ) : Closure
Create a callback that persists a model in the database when invoked.
Parameters
- $attributes array
- $parent Model |null
store ( Collection $results ) : void
Set the connection name on the results and store them.
Parameters
- $results Collection
createChildren ( Model $model ) : void
Create the children for the given model.
Parameters
- $model Model
makeOne ( $attributes ) : Model
Make a single instance of the model.
Parameters
- $attributes callable|array
make ( $attributes , Model $parent ) : Collection |Model
Create a collection of models.
Parameters
- $attributes array
- $parent Model |null
makeInstance ( Model $parent ) : Model
Make an instance of the model with the given attributes.
Parameters
- $parent Model |null
getExpandedAttributes ( Model $parent ) : mixed
Get a raw attributes array for the model.
Parameters
- $parent Model |null
getRawAttributes ( Model $parent ) : array
Get the raw attributes for the model as an array.
Parameters
- $parent Model |null
parentResolvers ( ) : array
Create the parent relationship resolvers (as deferred Closures).
expandAttributes ( array $definition ) : array
Expand all attributes to their underlying values.
Parameters
- $definition array
state ( $state ) : static
Add a new state transformation to the model definition.
Parameters
- $state callable|array
sequence ( $sequence ) : static
Add a new sequenced state transformation to the model definition.
Parameters
- $sequence array
crossJoinSequence ( $sequence ) : static
Add a new cross joined sequenced state transformation to the model definition.
Parameters
- $sequence array
has ( $factory , $relationship ) : static
Define a child relationship for the model.
Parameters
- $factory Factory
- $relationship string|null
guessRelationship ( string $related ) : string
Attempt to guess the relationship name for a "has" relationship.
Parameters
- $related string
hasAttached ( $factory , $pivot , $relationship ) : static
Define an attached relationship for the model.
Parameters
- $factory Factory |Collection |Model
- $pivot callable|array
- $relationship string|null
for ( $factory , $relationship ) : static
Define a parent relationship for the model.
Parameters
afterMaking ( Closure $callback ) : static
Add a new "after making" callback to the model definition.
Parameters
- $callback Closure
afterCreating ( Closure $callback ) : static
Add a new "after creating" callback to the model definition.
Parameters
- $callback Closure
callAfterMaking ( Collection $instances ) : void
Call the "after making" callbacks for the given model instances.
Parameters
- $instances Collection
callAfterCreating ( Collection $instances , Model $parent ) : void
Call the "after creating" callbacks for the given model instances.
Parameters
- $instances Collection
- $parent Model |null
count ( int $count ) : static
Specify how many models should be generated.
Parameters
- $count int|null
connection ( string $connection ) : static
Specify the database connection that should be used to generate models.
Parameters
- $connection string
newInstance ( array $arguments ) : static
Create a new instance of the factory builder with the given mutated properties.
Parameters
- $arguments array
newModel ( array $attributes ) : Model
Get a new model instance.
Parameters
- $attributes array
modelName ( ) : string
Get the name of the model that is generated by the factory.
guessModelNamesUsing ( callable $callback ) : void
Specify the callback that should be invoked to guess model names based on factory names.
Parameters
- $callback callable
useNamespace ( string $namespace ) : void
Specify the default namespace that contains the application's model factories.
Parameters
- $namespace string
factoryForModel ( string $modelName ) : static
Get a new factory instance for the given model name.
Parameters
- $modelName string
guessFactoryNamesUsing ( callable $callback ) : void
Specify the callback that should be invoked to guess factory names based on dynamic relationship names.
Parameters
- $callback callable
withFaker ( ) : Faker\Generator
Get a new Faker instance.
resolveFactoryName ( string $modelName ) : string
Get the factory name for the given model name.
Parameters
- $modelName string
appNamespace ( ) : string
Get the application namespace for the application.
__call ( $method , $parameters ) : mixed
Proxy dynamic factory methods onto their proper methods.
Parameters
- $method string
- $parameters array