trait Illuminate / Database / Eloquent / Concerns / HasAttributes

HasAttributes

Properties

protected

$attributes array

The model's attributes.

Default: []
protected

$original array

The model attribute's original state.

Default: []
protected

$changes array

The changed model attributes.

Default: []
protected

$casts array

The attributes that should be cast.

Default: []
protected

$classCastCache array

The attributes that have been cast using custom classes.

Default: []
protected

$attributeCastCache array

The attributes that have been cast using "Attribute" return type mutators.

Default: []
protected static

$primitiveCastTypes string[]

The built-in, primitive cast types supported by Eloquent.

Default: array
protected

$dateFormat string

The storage format of the model's date columns.

Default: null
protected

$appends array

The accessors to append to the model's array form.

Default: []
public static

$snakeAttributes bool

Indicates whether attributes are snake cased on arrays.

Default: true
protected static

$mutatorCache array

The cache of the mutated attributes for each class.

Default: []
protected static

$attributeMutatorCache array

The cache of the "Attribute" return type marked mutated attributes for each class.

Default: []
protected static

$getAttributeMutatorCache array

The cache of the "Attribute" return type marked mutated, gettable attributes for each class.

Default: []
protected static

$setAttributeMutatorCache array

The cache of the "Attribute" return type marked mutated, settable attributes for each class.

Default: []
protected static

$castTypeCache array

The cache of the converted cast types.

Default: []
public static

$encrypter Encrypter |null

The encrypter instance that is used to encrypt attributes.

Default: null

Methods

public

attributesToArray ( ) : array

Convert the model's attributes to an array.

protected

addDateAttributesToArray ( array $attributes ) : array

Add the date attributes to the attributes array.

Parameters

  • $attributes array
protected

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

Add the mutated attributes to the attributes array.

Parameters

  • $attributes array
  • $mutatedAttributes array
protected

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

Add the casted attributes to the attributes array.

Parameters

  • $attributes array
  • $mutatedAttributes array
protected

getArrayableAttributes ( ) : array

Get an attribute array of all arrayable attributes.

protected

getArrayableAppends ( ) : array

Get all of the appendable values that are arrayable.

public

relationsToArray ( ) : array

Get the model's relationships in array form.

protected

getArrayableRelations ( ) : array

Get an attribute array of all arrayable relations.

protected

getArrayableItems ( array $values ) : array

Get an attribute array of all arrayable values.

Parameters

  • $values array
public

getAttribute ( $key ) : mixed

Get an attribute from the model.

Parameters

  • $key string
protected

throwMissingAttributeExceptionIfApplicable ( $key ) : null

Either throw a missing attribute exception or return null depending on Eloquent's configuration.

Parameters

  • $key string
public

getAttributeValue ( $key ) : mixed

Get a plain attribute (not a relationship).

Parameters

  • $key string
protected

getAttributeFromArray ( $key ) : mixed

Get an attribute from the $attributes array.

Parameters

  • $key string
public

getRelationValue ( $key ) : mixed

Get a relationship.

Parameters

  • $key string
public

isRelation ( $key ) : bool

Determine if the given key is a relationship method on the model.

Parameters

  • $key string
protected

handleLazyLoadingViolation ( $key ) : mixed

Handle a lazy loading violation.

Parameters

  • $key string
protected

getRelationshipFromMethod ( $method ) : mixed

Get a relationship value from a method.

Parameters

  • $method string
public

hasGetMutator ( $key ) : bool

Determine if a get mutator exists for an attribute.

Parameters

  • $key string
public

hasAttributeMutator ( $key ) : bool

Determine if a "Attribute" return type marked mutator exists for an attribute.

Parameters

  • $key string
public

hasAttributeGetMutator ( $key ) : bool

Determine if a "Attribute" return type marked get mutator exists for an attribute.

Parameters

  • $key string
protected

mutateAttribute ( $key , $value ) : mixed

Get the value of an attribute using its mutator.

Parameters

  • $key string
  • $value mixed
protected

mutateAttributeMarkedAttribute ( $key , $value ) : mixed

Get the value of an "Attribute" return type marked attribute using its mutator.

Parameters

  • $key string
  • $value mixed
protected

mutateAttributeForArray ( $key , $value ) : mixed

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

Parameters

  • $key string
  • $value mixed
public

mergeCasts ( $casts ) : $this

Merge new casts with existing casts on the model.

Parameters

  • $casts array
protected

castAttribute ( $key , $value ) : mixed

Cast an attribute to a native PHP type.

Parameters

  • $key string
  • $value mixed
protected

getClassCastableAttributeValue ( $key , $value ) : mixed

Cast the given attribute using a custom cast class.

Parameters

  • $key string
  • $value mixed
protected

getEnumCastableAttributeValue ( $key , $value ) : mixed

Cast the given attribute to an enum.

Parameters

  • $key string
  • $value mixed
protected

getCastType ( $key ) : string

Get the type of cast for a model attribute.

Parameters

  • $key string
protected

deviateClassCastableAttribute ( $method , $key , $value ) : mixed

Increment or decrement the given attribute using the custom cast class.

Parameters

  • $method string
  • $key string
  • $value mixed
protected

serializeClassCastableAttribute ( $key , $value ) : mixed

Serialize the given attribute using the custom cast class.

Parameters

  • $key string
  • $value mixed
protected

isCustomDateTimeCast ( $cast ) : bool

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

Parameters

  • $cast string
protected

isImmutableCustomDateTimeCast ( $cast ) : bool

Determine if the cast type is an immutable custom date time cast.

Parameters

  • $cast string
protected

isDecimalCast ( $cast ) : bool

Determine if the cast type is a decimal cast.

Parameters

  • $cast string
public

setAttribute ( $key , $value ) : mixed

Set a given attribute on the model.

Parameters

  • $key string
  • $value mixed
public

hasSetMutator ( $key ) : bool

Determine if a set mutator exists for an attribute.

Parameters

  • $key string
public

hasAttributeSetMutator ( $key ) : bool

Determine if an "Attribute" return type marked set mutator exists for an attribute.

Parameters

  • $key string
protected

setMutatedAttributeValue ( $key , $value ) : mixed

Set the value of an attribute using its mutator.

Parameters

  • $key string
  • $value mixed
protected

setAttributeMarkedMutatedAttributeValue ( $key , $value ) : mixed

Set the value of a "Attribute" return type marked attribute using its mutator.

Parameters

  • $key string
  • $value mixed
protected

isDateAttribute ( $key ) : bool

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

Parameters

  • $key string
public

fillJsonAttribute ( $key , $value ) : $this

Set a given JSON attribute on the model.

Parameters

  • $key string
  • $value mixed
protected

setClassCastableAttribute ( $key , $value ) : void

Set the value of a class castable attribute.

Parameters

  • $key string
  • $value mixed
protected

setEnumCastableAttribute ( $key , $value ) : void

Set the value of an enum castable attribute.

Parameters

  • $key string
  • $value UnitEnum|string|int
protected

getEnumCaseFromValue ( $enumClass , $value ) : UnitEnum|BackedEnum

Get an enum case instance from a given class and value.

Parameters

  • $enumClass string
  • $value string|int
protected

getStorableEnumValue ( $value ) : string|int

Get the storable value from the given enum.

Parameters

  • $value UnitEnum|BackedEnum
protected

getArrayAttributeWithValue ( $path , $key , $value ) : $this

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

Parameters

  • $path string
  • $key string
  • $value mixed
protected

getArrayAttributeByKey ( $key ) : array

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

Parameters

  • $key string
protected

castAttributeAsJson ( $key , $value ) : string

Cast the given attribute to JSON.

Parameters

  • $key string
  • $value mixed
protected

asJson ( $value ) : string

Encode the given value as JSON.

Parameters

  • $value mixed
public

fromJson ( $value , $asObject ) : mixed

Decode the given JSON back into an array or object.

Parameters

  • $value string
  • $asObject bool
public

fromEncryptedString ( $value ) : mixed

Decrypt the given encrypted string.

Parameters

  • $value string
protected

castAttributeAsEncryptedString ( $key , $value ) : string

Cast the given attribute to an encrypted string.

Parameters

  • $key string
  • $value mixed
public static

encryptUsing ( $encrypter ) : void

Set the encrypter instance that will be used to encrypt attributes.

Parameters

protected

castAttributeAsHashedString ( $key , $value ) : string

Cast the given attribute to a hashed string.

Parameters

  • $key string
  • $value mixed
public

fromFloat ( $value ) : mixed

Decode the given float.

Parameters

  • $value mixed
protected

asDecimal ( $value , $decimals ) : string

Return a decimal as string.

Parameters

  • $value float|string
  • $decimals int
protected

asDate ( $value ) : Carbon

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

Parameters

  • $value mixed
protected

asDateTime ( $value ) : Carbon

Return a timestamp as DateTime object.

Parameters

  • $value mixed
protected

isStandardDateFormat ( $value ) : bool

Determine if the given value is a standard date format.

Parameters

  • $value string
public

fromDateTime ( $value ) : string|null

Convert a DateTime to a storable string.

Parameters

  • $value mixed
protected

asTimestamp ( $value ) : int

Return a timestamp as unix timestamp.

Parameters

  • $value mixed
protected

serializeDate ( DateTimeInterface $date ) : string

Prepare a date for array / JSON serialization.

Parameters

public

getDates ( ) : array

Get the attributes that should be converted to dates.

public

getDateFormat ( ) : string

Get the format for database stored dates.

public

setDateFormat ( $format ) : $this

Set the date format used by the model.

Parameters

  • $format string
public

hasCast ( $key , $types ) : bool

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

Parameters

  • $key string
  • $types array|string|null
public

getCasts ( ) : array

Get the casts array.

protected

isDateCastable ( $key ) : bool

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

Parameters

  • $key string
protected

isDateCastableWithCustomFormat ( $key ) : bool

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

Parameters

  • $key string
protected

isJsonCastable ( $key ) : bool

Determine whether a value is JSON castable for inbound manipulation.

Parameters

  • $key string
protected

isEncryptedCastable ( $key ) : bool

Determine whether a value is an encrypted castable for inbound manipulation.

Parameters

  • $key string
protected

isClassCastable ( $key ) : bool

Determine if the given key is cast using a custom class.

Parameters

  • $key string
protected

isEnumCastable ( $key ) : bool

Determine if the given key is cast using an enum.

Parameters

  • $key string
protected

isClassDeviable ( $key ) : bool

Determine if the key is deviable using a custom class.

Parameters

  • $key string
protected

isClassSerializable ( $key ) : bool

Determine if the key is serializable using a custom class.

Parameters

  • $key string
protected

resolveCasterClass ( $key ) : mixed

Resolve the custom caster class for a given key.

Parameters

  • $key string
protected

parseCasterClass ( $class ) : string

Parse the given caster class, removing any arguments.

Parameters

  • $class string
protected

mergeAttributesFromCachedCasts ( ) : void

Merge the cast class and attribute cast attributes back into the model.

protected

mergeAttributesFromClassCasts ( ) : void

Merge the cast class attributes back into the model.

protected

mergeAttributesFromAttributeCasts ( ) : void

Merge the cast class attributes back into the model.

protected

normalizeCastClassResponse ( $key , $value ) : array

Normalize the response from a custom class caster.

Parameters

  • $key string
  • $value mixed
public

getAttributes ( ) : array

Get all of the current attributes on the model.

protected

getAttributesForInsert ( ) : array

Get all of the current attributes on the model for an insert operation.

public

setRawAttributes ( array $attributes , $sync ) : $this

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

Parameters

  • $attributes array
  • $sync bool
public

getOriginal ( $key , $default ) : mixed|array

Get the model's original attribute values.

Parameters

  • $key string|null
  • $default mixed
protected

getOriginalWithoutRewindingModel ( $key , $default ) : mixed|array

Get the model's original attribute values.

Parameters

  • $key string|null
  • $default mixed
public

getRawOriginal ( $key , $default ) : mixed|array

Get the model's raw original attribute values.

Parameters

  • $key string|null
  • $default mixed
public

only ( $attributes ) : array

Get a subset of the model's attributes.

Parameters

  • $attributes array|mixed
public

syncOriginal ( ) : $this

Sync the original attributes with the current.

public

syncOriginalAttribute ( $attribute ) : $this

Sync a single original attribute with its current value.

Parameters

  • $attribute string
public

syncOriginalAttributes ( $attributes ) : $this

Sync multiple original attribute with their current values.

Parameters

  • $attributes array|string
public

syncChanges ( ) : $this

Sync the changed attributes.

public

isDirty ( $attributes ) : bool

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

Parameters

  • $attributes array|string|null
public

isClean ( $attributes ) : bool

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

Parameters

  • $attributes array|string|null
public

discardChanges ( ) : $this

Discard attribute changes and reset the attributes to their original state.

public

wasChanged ( $attributes ) : bool

Determine if the model or any of the given attribute(s) were changed when the model was last saved.

Parameters

  • $attributes array|string|null
protected

hasChanges ( $changes , $attributes ) : bool

Determine if any of the given attributes were changed when the model was last saved.

Parameters

  • $changes array
  • $attributes array|string|null
public

getDirty ( ) : array

Get the attributes that have been changed since the last sync.

public

getChanges ( ) : array

Get the attributes that were changed when the model was last saved.

public

originalIsEquivalent ( $key ) : bool

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

Parameters

  • $key string
protected

transformModelValue ( $key , $value ) : mixed

Transform a raw model value using mutators, casts, etc.

Parameters

  • $key string
  • $value mixed
public

append ( $attributes ) : $this

Append attributes to query when building a query.

Parameters

  • $attributes array|string
public

getAppends ( ) : array

Get the accessors that are being appended to model arrays.

public

setAppends ( array $appends ) : $this

Set the accessors to append to model arrays.

Parameters

  • $appends array
public

hasAppended ( $attribute ) : bool

Return whether the accessor attribute has been appended.

Parameters

  • $attribute string
public

getMutatedAttributes ( ) : array

Get the mutated attributes for a given instance.

public static

cacheMutatedAttributes ( $classOrInstance ) : void

Extract and cache all the mutated attributes of a class.

Parameters

  • $classOrInstance object|string
protected static

getMutatorMethods ( $class ) : array

Get all of the attribute mutator methods.

Parameters

  • $class mixed
protected static

getAttributeMarkedMutatorMethods ( $class ) : array

Get all of the "Attribute" return typed attribute mutator methods.

Parameters

  • $class mixed