You are viewing an older version of the documentation. For the latest, please visit master documentation.
abstract

Factory

Uses

Properties

protected

$model string|null

The name of the factory's corresponding model.

Default: null
protected

$count int|null

The number of models that should be generated.

Default: null
protected

$states Collection

The state transformations that will be applied to the model.

Default: null
protected

$has Collection

The parent relationships that will be applied to the model.

Default: null
protected

$for Collection

The child relationships that will be applied to the model.

Default: null
protected

$afterMaking Collection

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

Default: null
protected

$afterCreating Collection

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

Default: null
protected

$connection string

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

Default: null
protected

$faker Faker\Generator

The current Faker instance.

Default: null
protected static

$namespace string

The default namespace where factories reside.

Default: 'Database\Factories\'
protected static

$modelNameResolver callable

The default model name resolver.

Default: null
protected static

$factoryNameResolver callable

The factory name resolver.

Default: null

Methods

public

__construct ( $count , Collection $states , Collection $has , Collection $for , Collection $afterMaking , Collection $afterCreating , $connection ) : void

Create a new factory instance.

Parameters

public abstract

definition ( ) : array

Define the model's default state.

public static

new ( $attributes ) : static

Get a new factory instance for the given attributes.

Parameters

  • $attributes callable|array
public static

times ( int $count ) : static

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

Parameters

  • $count int
public

configure ( ) : $this

Configure the factory.

public

raw ( $attributes , Model $parent ) : array

Get the raw attributes generated by the factory.

Parameters

  • $attributes array
  • $parent Model |null
public

createOne ( $attributes ) : Model

Create a single model and persist it to the database.

Parameters

  • $attributes array
public

createOneQuietly ( $attributes ) : Model

Create a single model and persist it to the database.

Parameters

  • $attributes array
public

createMany ( iterable $records ) : Collection

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

Parameters

  • $records iterable
public

createManyQuietly ( iterable $records ) : Collection

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

Parameters

  • $records iterable
public

create ( $attributes , Model $parent ) : Collection |Model

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

Parameters

  • $attributes array
  • $parent Model |null
public

createQuietly ( $attributes , Model $parent ) : Collection |Model

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

Parameters

  • $attributes array
  • $parent Model |null
public

lazy ( array $attributes , Model $parent ) : Closure

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

Parameters

  • $attributes array
  • $parent Model |null
protected

store ( Collection $results ) : void

Set the connection name on the results and store them.

Parameters

protected

createChildren ( Model $model ) : void

Create the children for the given model.

Parameters

public

makeOne ( $attributes ) : Model

Make a single instance of the model.

Parameters

  • $attributes callable|array
public

make ( $attributes , Model $parent ) : Collection |Model

Create a collection of models.

Parameters

  • $attributes array
  • $parent Model |null
protected

makeInstance ( Model $parent ) : Model

Make an instance of the model with the given attributes.

Parameters

protected

getExpandedAttributes ( Model $parent ) : mixed

Get a raw attributes array for the model.

Parameters

protected

getRawAttributes ( Model $parent ) : array

Get the raw attributes for the model as an array.

Parameters

protected

parentResolvers ( ) : array

Create the parent relationship resolvers (as deferred Closures).

protected

expandAttributes ( array $definition ) : array

Expand all attributes to their underlying values.

Parameters

  • $definition array
public

state ( $state ) : static

Add a new state transformation to the model definition.

Parameters

  • $state callable|array
public

sequence ( $sequence ) : static

Add a new sequenced state transformation to the model definition.

Parameters

  • $sequence array
public

crossJoinSequence ( $sequence ) : static

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

Parameters

  • $sequence array
public

has ( $factory , $relationship ) : static

Define a child relationship for the model.

Parameters

  • $factory Factory
  • $relationship string|null
protected

guessRelationship ( string $related ) : string

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

Parameters

  • $related string
public

hasAttached ( $factory , $pivot , $relationship ) : static

Define an attached relationship for the model.

Parameters

public

for ( $factory , $relationship ) : static

Define a parent relationship for the model.

Parameters

public

afterMaking ( Closure $callback ) : static

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

Parameters

public

afterCreating ( Closure $callback ) : static

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

Parameters

protected

callAfterMaking ( Collection $instances ) : void

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

Parameters

protected

callAfterCreating ( Collection $instances , Model $parent ) : void

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

Parameters

public

count ( int $count ) : static

Specify how many models should be generated.

Parameters

  • $count int|null
public

connection ( string $connection ) : static

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

Parameters

  • $connection string
protected

newInstance ( array $arguments ) : static

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

Parameters

  • $arguments array
public

newModel ( array $attributes ) : Model

Get a new model instance.

Parameters

  • $attributes array
public

modelName ( ) : string

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

public static

guessModelNamesUsing ( callable $callback ) : void

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

Parameters

  • $callback callable
public static

useNamespace ( string $namespace ) : void

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

Parameters

  • $namespace string
public static

factoryForModel ( string $modelName ) : static

Get a new factory instance for the given model name.

Parameters

  • $modelName string
public static

guessFactoryNamesUsing ( callable $callback ) : void

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

Parameters

  • $callback callable
protected

withFaker ( ) : Faker\Generator

Get a new Faker instance.

public static

resolveFactoryName ( string $modelName ) : string

Get the factory name for the given model name.

Parameters

  • $modelName string
protected static

appNamespace ( ) : string

Get the application namespace for the application.

public

__call ( $method , $parameters ) : mixed

Proxy dynamic factory methods onto their proper methods.

Parameters

  • $method string
  • $parameters array