class Factory implements ArrayAccess (View source)

Properties

protected array $definitions

The model definitions in the container.

protected array $states

The registered model states.

protected array $afterMaking

The registered after making callbacks.

protected array $afterCreating

The registered after creating callbacks.

protected Generator $faker

The Faker instance for the builder.

Methods

void
__construct(Generator $faker)

Create a new factory instance.

static Factory
construct(Generator $faker, string|null $pathToFactories = null)

Create a new factory container.

$this
define(string $class, callable $attributes)

Define a class with a given set of attributes.

$this
state(string $class, string $state, callable|array $attributes)

Define a state with a given set of attributes.

$this
afterMaking(string $class, callable $callback, string $name = 'default')

Define a callback to run after making a model.

$this
afterMakingState(string $class, string $state, callable $callback)

Define a callback to run after making a model with given state.

$this
afterCreating(string $class, callable $callback, string $name = 'default')

Define a callback to run after creating a model.

$this
afterCreatingState(string $class, string $state, callable $callback)

Define a callback to run after creating a model with given state.

mixed
create(string $class, array $attributes = [])

Create an instance of the given model and persist it to the database.

mixed
make(string $class, array $attributes = [])

Create an instance of the given model.

array
raw(string $class, array $attributes = [])

Get the raw attribute array for a given model.

of(string $class)

Create a builder for the given model.

$this
load(string $path)

Load factories from path.

bool
offsetExists(string $offset)

Determine if the given offset exists.

mixed
offsetGet(string $offset)

Get the value of the given offset.

void
offsetSet(string $offset, callable $value)

Set the given offset to the given value.

void
offsetUnset(string $offset)

Unset the value at the given offset.

Details

void __construct(Generator $faker)

Create a new factory instance.

Parameters

Generator $faker

Return Value

void

static Factory construct(Generator $faker, string|null $pathToFactories = null)

Create a new factory container.

Parameters

Generator $faker
string|null $pathToFactories

Return Value

Factory

$this define(string $class, callable $attributes)

Define a class with a given set of attributes.

Parameters

string $class
callable $attributes

Return Value

$this

$this state(string $class, string $state, callable|array $attributes)

Define a state with a given set of attributes.

Parameters

string $class
string $state
callable|array $attributes

Return Value

$this

$this afterMaking(string $class, callable $callback, string $name = 'default')

Define a callback to run after making a model.

Parameters

string $class
callable $callback
string $name

Return Value

$this

$this afterMakingState(string $class, string $state, callable $callback)

Define a callback to run after making a model with given state.

Parameters

string $class
string $state
callable $callback

Return Value

$this

$this afterCreating(string $class, callable $callback, string $name = 'default')

Define a callback to run after creating a model.

Parameters

string $class
callable $callback
string $name

Return Value

$this

$this afterCreatingState(string $class, string $state, callable $callback)

Define a callback to run after creating a model with given state.

Parameters

string $class
string $state
callable $callback

Return Value

$this

mixed create(string $class, array $attributes = [])

Create an instance of the given model and persist it to the database.

Parameters

string $class
array $attributes

Return Value

mixed

mixed make(string $class, array $attributes = [])

Create an instance of the given model.

Parameters

string $class
array $attributes

Return Value

mixed

array raw(string $class, array $attributes = [])

Get the raw attribute array for a given model.

Parameters

string $class
array $attributes

Return Value

array

FactoryBuilder of(string $class)

Create a builder for the given model.

Parameters

string $class

Return Value

FactoryBuilder

$this load(string $path)

Load factories from path.

Parameters

string $path

Return Value

$this

bool offsetExists(string $offset)

Determine if the given offset exists.

Parameters

string $offset

Return Value

bool

mixed offsetGet(string $offset)

Get the value of the given offset.

Parameters

string $offset

Return Value

mixed

void offsetSet(string $offset, callable $value)

Set the given offset to the given value.

Parameters

string $offset
callable $value

Return Value

void

void offsetUnset(string $offset)

Unset the value at the given offset.

Parameters

string $offset

Return Value

void

Laravel Cloud is the best place to deploy Laravel, Nuxt, Express, Hono, Node.js, Bun, Go, Flask, Python, and more, with dedicated infrastructure available on AWS through Laravel Private Cloud.