HasAttributes

trait HasAttributes (View source)

Properties

protected array $attributes The model's attributes.
protected array $original The model attribute's original state.
protected array $changes The changed model attributes.
protected array $casts The attributes that should be cast to native types.
protected array $dates The attributes that should be mutated to dates.
protected string $dateFormat The storage format of the model's date columns.
protected array $appends The accessors to append to the model's array form.
static bool $snakeAttributes Indicates whether attributes are snake cased on arrays.
static protected array $mutatorCache The cache of the mutated attributes for each class.

Methods

array attributesToArray()

Convert the model's attributes to an array.

array addDateAttributesToArray(array $attributes)

Add the date attributes to the attributes array.

array addMutatedAttributesToArray(array $attributes, array $mutatedAttributes)

Add the mutated attributes to the attributes array.

array addCastAttributesToArray(array $attributes, array $mutatedAttributes)

Add the casted attributes to the attributes array.

array getArrayableAttributes()

Get an attribute array of all arrayable attributes.

array getArrayableAppends()

Get all of the appendable values that are arrayable.

array relationsToArray()

Get the model's relationships in array form.

array getArrayableRelations()

Get an attribute array of all arrayable relations.

array getArrayableItems(array $values)

Get an attribute array of all arrayable values.

mixed getAttribute(string $key)

Get an attribute from the model.

mixed getAttributeValue(string $key)

Get a plain attribute (not a relationship).

mixed getAttributeFromArray(string $key)

Get an attribute from the $attributes array.

mixed getRelationValue(string $key)

Get a relationship.

mixed getRelationshipFromMethod(string $method)

Get a relationship value from a method.

bool hasGetMutator(string $key)

Determine if a get mutator exists for an attribute.

mixed mutateAttribute(string $key, mixed $value)

Get the value of an attribute using its mutator.

mixed mutateAttributeForArray(string $key, mixed $value)

Get the value of an attribute using its mutator for array conversion.

mixed castAttribute(string $key, mixed $value)

Cast an attribute to a native PHP type.

string getCastType(string $key)

Get the type of cast for a model attribute.

bool isCustomDateTimeCast(string $cast)

Determine if the cast type is a custom date time cast.

mixed setAttribute(string $key, mixed $value)

Set a given attribute on the model.

bool hasSetMutator(string $key)

Determine if a set mutator exists for an attribute.

mixed setMutatedAttributeValue(string $key, mixed $value)

Set the value of an attribute using its mutator.

bool isDateAttribute(string $key)

Determine if the given attribute is a date or date castable.

$this fillJsonAttribute(string $key, mixed $value)

Set a given JSON attribute on the model.

$this getArrayAttributeWithValue(string $path, string $key, mixed $value)

Get an array attribute with the given key and value set.

array getArrayAttributeByKey(string $key)

Get an array attribute or return an empty array if it is not set.

string castAttributeAsJson(string $key, mixed $value)

Cast the given attribute to JSON.

string asJson(mixed $value)

Encode the given value as JSON.

mixed fromFloat(mixed $value)

Decode the given float.

mixed fromJson(string $value, bool $asObject = false)

Decode the given JSON back into an array or object.

Carbon asDate(mixed $value)

Return a timestamp as DateTime object with time set to 00:00:00.

Carbon asDateTime(mixed $value)

Return a timestamp as DateTime object.

bool isStandardDateFormat(string $value)

Determine if the given value is a standard date format.

string fromDateTime(DateTime|int $value)

Convert a DateTime to a storable string.

int asTimestamp(mixed $value)

Return a timestamp as unix timestamp.

string serializeDate(DateTimeInterface $date)

Prepare a date for array / JSON serialization.

array getDates()

Get the attributes that should be converted to dates.

string getDateFormat()

Get the format for database stored dates.

$this setDateFormat(string $format)

Set the date format used by the model.

bool hasCast(string $key, array|string|null $types = null)

Determine whether an attribute should be cast to a native type.

array getCasts()

Get the casts array.

bool isDateCastable(string $key)

Determine whether a value is Date / DateTime castable for inbound manipulation.

bool isJsonCastable(string $key)

Determine whether a value is JSON castable for inbound manipulation.

array getAttributes()

Get all of the current attributes on the model.

$this setRawAttributes(array $attributes, bool $sync = false)

Set the array of model attributes. No checking is done.

mixed|array getOriginal(string|null $key = null, mixed $default = null)

Get the model's original attribute values.

array only(array|mixed $attributes)

Get a subset of the model's attributes.

$this syncOriginal()

Sync the original attributes with the current.

$this syncOriginalAttribute(string $attribute)

Sync a single original attribute with its current value.

$this syncChanges()

Sync the changed attributes.

bool isDirty(array|string|null $attributes = null)

Determine if the model or given attribute(s) have been modified.

bool isClean(array|string|null $attributes = null)

Determine if the model or given attribute(s) have remained the same.

bool wasChanged(array|string|null $attributes = null)

Determine if the model or given attribute(s) have been modified.

bool hasChanges(array $changes, array|string|null $attributes = null)

Determine if the given attributes were changed.

array getDirty()

Get the attributes that have been changed since last sync.

array getChanges()

Get the attributes that were changed.

bool originalIsEquivalent(string $key, mixed $current)

Determine if the new and old values for a given key are equivalent.

$this append(array|string $attributes)

Append attributes to query when building a query.

$this setAppends(array $appends)

Set the accessors to append to model arrays.

array getMutatedAttributes()

Get the mutated attributes for a given instance.

static void cacheMutatedAttributes(string $class)

Extract and cache all the mutated attributes of a class.

static array getMutatorMethods(mixed $class)

Get all of the attribute mutator methods.

Details

array attributesToArray()

Convert the model's attributes to an array.

Return Value

array

protected array addDateAttributesToArray(array $attributes)

Add the date attributes to the attributes array.

Parameters

array $attributes

Return Value

array

protected array addMutatedAttributesToArray(array $attributes, array $mutatedAttributes)

Add the mutated attributes to the attributes array.

Parameters

array $attributes
array $mutatedAttributes

Return Value

array

protected array addCastAttributesToArray(array $attributes, array $mutatedAttributes)

Add the casted attributes to the attributes array.

Parameters

array $attributes
array $mutatedAttributes

Return Value

array

protected array getArrayableAttributes()

Get an attribute array of all arrayable attributes.

Return Value

array

protected array getArrayableAppends()

Get all of the appendable values that are arrayable.

Return Value

array

array relationsToArray()

Get the model's relationships in array form.

Return Value

array

protected array getArrayableRelations()

Get an attribute array of all arrayable relations.

Return Value

array

protected array getArrayableItems(array $values)

Get an attribute array of all arrayable values.

Parameters

array $values

Return Value

array

mixed getAttribute(string $key)

Get an attribute from the model.

Parameters

string $key

Return Value

mixed

mixed getAttributeValue(string $key)

Get a plain attribute (not a relationship).

Parameters

string $key

Return Value

mixed

protected mixed getAttributeFromArray(string $key)

Get an attribute from the $attributes array.

Parameters

string $key

Return Value

mixed

mixed getRelationValue(string $key)

Get a relationship.

Parameters

string $key

Return Value

mixed

protected mixed getRelationshipFromMethod(string $method)

Get a relationship value from a method.

Parameters

string $method

Return Value

mixed

Exceptions

LogicException

bool hasGetMutator(string $key)

Determine if a get mutator exists for an attribute.

Parameters

string $key

Return Value

bool

protected mixed mutateAttribute(string $key, mixed $value)

Get the value of an attribute using its mutator.

Parameters

string $key
mixed $value

Return Value

mixed

protected mixed mutateAttributeForArray(string $key, mixed $value)

Get the value of an attribute using its mutator for array conversion.

Parameters

string $key
mixed $value

Return Value

mixed

protected mixed castAttribute(string $key, mixed $value)

Cast an attribute to a native PHP type.

Parameters

string $key
mixed $value

Return Value

mixed

protected string getCastType(string $key)

Get the type of cast for a model attribute.

Parameters

string $key

Return Value

string

protected bool isCustomDateTimeCast(string $cast)

Determine if the cast type is a custom date time cast.

Parameters

string $cast

Return Value

bool

mixed setAttribute(string $key, mixed $value)

Set a given attribute on the model.

Parameters

string $key
mixed $value

Return Value

mixed

bool hasSetMutator(string $key)

Determine if a set mutator exists for an attribute.

Parameters

string $key

Return Value

bool

protected mixed setMutatedAttributeValue(string $key, mixed $value)

Set the value of an attribute using its mutator.

Parameters

string $key
mixed $value

Return Value

mixed

protected bool isDateAttribute(string $key)

Determine if the given attribute is a date or date castable.

Parameters

string $key

Return Value

bool

$this fillJsonAttribute(string $key, mixed $value)

Set a given JSON attribute on the model.

Parameters

string $key
mixed $value

Return Value

$this

protected $this getArrayAttributeWithValue(string $path, string $key, mixed $value)

Get an array attribute with the given key and value set.

Parameters

string $path
string $key
mixed $value

Return Value

$this

protected array getArrayAttributeByKey(string $key)

Get an array attribute or return an empty array if it is not set.

Parameters

string $key

Return Value

array

protected string castAttributeAsJson(string $key, mixed $value)

Cast the given attribute to JSON.

Parameters

string $key
mixed $value

Return Value

string

protected string asJson(mixed $value)

Encode the given value as JSON.

Parameters

mixed $value

Return Value

string

mixed fromFloat(mixed $value)

Decode the given float.

Parameters

mixed $value

Return Value

mixed

mixed fromJson(string $value, bool $asObject = false)

Decode the given JSON back into an array or object.

Parameters

string $value
bool $asObject

Return Value

mixed

protected Carbon asDate(mixed $value)

Return a timestamp as DateTime object with time set to 00:00:00.

Parameters

mixed $value

Return Value

Carbon

protected Carbon asDateTime(mixed $value)

Return a timestamp as DateTime object.

Parameters

mixed $value

Return Value

Carbon

protected bool isStandardDateFormat(string $value)

Determine if the given value is a standard date format.

Parameters

string $value

Return Value

bool

string fromDateTime(DateTime|int $value)

Convert a DateTime to a storable string.

Parameters

DateTime|int $value

Return Value

string

protected int asTimestamp(mixed $value)

Return a timestamp as unix timestamp.

Parameters

mixed $value

Return Value

int

protected string serializeDate(DateTimeInterface $date)

Prepare a date for array / JSON serialization.

Parameters

DateTimeInterface $date

Return Value

string

array getDates()

Get the attributes that should be converted to dates.

Return Value

array

string getDateFormat()

Get the format for database stored dates.

Return Value

string

$this setDateFormat(string $format)

Set the date format used by the model.

Parameters

string $format

Return Value

$this

bool hasCast(string $key, array|string|null $types = null)

Determine whether an attribute should be cast to a native type.

Parameters

string $key
array|string|null $types

Return Value

bool

array getCasts()

Get the casts array.

Return Value

array

protected bool isDateCastable(string $key)

Determine whether a value is Date / DateTime castable for inbound manipulation.

Parameters

string $key

Return Value

bool

protected bool isJsonCastable(string $key)

Determine whether a value is JSON castable for inbound manipulation.

Parameters

string $key

Return Value

bool

array getAttributes()

Get all of the current attributes on the model.

Return Value

array

$this setRawAttributes(array $attributes, bool $sync = false)

Set the array of model attributes. No checking is done.

Parameters

array $attributes
bool $sync

Return Value

$this

mixed|array getOriginal(string|null $key = null, mixed $default = null)

Get the model's original attribute values.

Parameters

string|null $key
mixed $default

Return Value

mixed|array

array only(array|mixed $attributes)

Get a subset of the model's attributes.

Parameters

array|mixed $attributes

Return Value

array

$this syncOriginal()

Sync the original attributes with the current.

Return Value

$this

$this syncOriginalAttribute(string $attribute)

Sync a single original attribute with its current value.

Parameters

string $attribute

Return Value

$this

$this syncChanges()

Sync the changed attributes.

Return Value

$this

bool isDirty(array|string|null $attributes = null)

Determine if the model or given attribute(s) have been modified.

Parameters

array|string|null $attributes

Return Value

bool

bool isClean(array|string|null $attributes = null)

Determine if the model or given attribute(s) have remained the same.

Parameters

array|string|null $attributes

Return Value

bool

bool wasChanged(array|string|null $attributes = null)

Determine if the model or given attribute(s) have been modified.

Parameters

array|string|null $attributes

Return Value

bool

protected bool hasChanges(array $changes, array|string|null $attributes = null)

Determine if the given attributes were changed.

Parameters

array $changes
array|string|null $attributes

Return Value

bool

array getDirty()

Get the attributes that have been changed since last sync.

Return Value

array

array getChanges()

Get the attributes that were changed.

Return Value

array

protected bool originalIsEquivalent(string $key, mixed $current)

Determine if the new and old values for a given key are equivalent.

Parameters

string $key
mixed $current

Return Value

bool

$this append(array|string $attributes)

Append attributes to query when building a query.

Parameters

array|string $attributes

Return Value

$this

$this setAppends(array $appends)

Set the accessors to append to model arrays.

Parameters

array $appends

Return Value

$this

array getMutatedAttributes()

Get the mutated attributes for a given instance.

Return Value

array

static void cacheMutatedAttributes(string $class)

Extract and cache all the mutated attributes of a class.

Parameters

string $class

Return Value

void

static protected array getMutatorMethods(mixed $class)

Get all of the attribute mutator methods.

Parameters

mixed $class

Return Value

array

© Taylor Otwell
Licensed under the MIT License.
Laravel is a trademark of Taylor Otwell.
https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasAttributes.html