On this page
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. |
|
protected array | $classCastCache | The attributes that have been cast using custom classes. |
|
protected array | $attributeCastCache | The attributes that have been cast using "Attribute" return type mutators. |
|
static protected string[] | $primitiveCastTypes | The built-in, primitive cast types supported by Eloquent. |
|
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. |
|
static protected array | $attributeMutatorCache | The cache of the "Attribute" return type marked mutated attributes for each class. |
|
static protected array | $getAttributeMutatorCache | The cache of the "Attribute" return type marked mutated, gettable attributes for each class. |
|
static protected array | $setAttributeMutatorCache | The cache of the "Attribute" return type marked mutated, settable attributes for each class. |
|
static protected array | $castTypeCache | The cache of the converted cast types. |
|
static Encrypter|null | $encrypter | The encrypter instance that is used to encrypt attributes. |
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. |
|
null | throwMissingAttributeExceptionIfApplicable(string $key) Either throw a missing attribute exception or return null depending on Eloquent's configuration. |
|
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. |
|
bool | isRelation(string $key) Determine if the given key is a relationship method on the model. |
|
mixed | handleLazyLoadingViolation(string $key) Handle a lazy loading violation. |
|
mixed | getRelationshipFromMethod(string $method) Get a relationship value from a method. |
|
bool | hasGetMutator(string $key) Determine if a get mutator exists for an attribute. |
|
bool | hasAttributeMutator(string $key) Determine if a "Attribute" return type marked mutator exists for an attribute. |
|
bool | hasAttributeGetMutator(string $key) Determine if a "Attribute" return type marked get mutator exists for an attribute. |
|
mixed | mutateAttribute(string $key, mixed $value) Get the value of an attribute using its mutator. |
|
mixed | mutateAttributeMarkedAttribute(string $key, mixed $value) Get the value of an "Attribute" return type marked attribute using its mutator. |
|
mixed | mutateAttributeForArray(string $key, mixed $value) Get the value of an attribute using its mutator for array conversion. |
|
$this | mergeCasts(array $casts) Merge new casts with existing casts on the model. |
|
mixed | castAttribute(string $key, mixed $value) Cast an attribute to a native PHP type. |
|
mixed | getClassCastableAttributeValue(string $key, mixed $value) Cast the given attribute using a custom cast class. |
|
mixed | getEnumCastableAttributeValue(string $key, mixed $value) Cast the given attribute to an enum. |
|
string | getCastType(string $key) Get the type of cast for a model attribute. |
|
mixed | deviateClassCastableAttribute(string $method, string $key, mixed $value) Increment or decrement the given attribute using the custom cast class. |
|
mixed | serializeClassCastableAttribute(string $key, mixed $value) Serialize the given attribute using the custom cast class. |
|
bool | isCustomDateTimeCast(string $cast) Determine if the cast type is a custom date time cast. |
|
bool | isImmutableCustomDateTimeCast(string $cast) Determine if the cast type is an immutable custom date time cast. |
|
bool | isDecimalCast(string $cast) Determine if the cast type is a decimal 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. |
|
bool | hasAttributeSetMutator(string $key) Determine if an "Attribute" return type marked set mutator exists for an attribute. |
|
mixed | setMutatedAttributeValue(string $key, mixed $value) Set the value of an attribute using its mutator. |
|
mixed | setAttributeMarkedMutatedAttributeValue(string $key, mixed $value) Set the value of a "Attribute" return type marked 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. |
|
void | setClassCastableAttribute(string $key, mixed $value) Set the value of a class castable attribute. |
|
void | setEnumCastableAttribute(string $key, UnitEnum|string|int $value) Set the value of an enum castable attribute. |
|
UnitEnum|BackedEnum | getEnumCaseFromValue(string $enumClass, string|int $value) Get an enum case instance from a given class and value. |
|
string|int | getStorableEnumValue(UnitEnum|BackedEnum $value) Get the storable value from the given enum. |
|
$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 | fromJson(string $value, bool $asObject = false) Decode the given JSON back into an array or object. |
|
mixed | fromEncryptedString(string $value) Decrypt the given encrypted string. |
|
string | castAttributeAsEncryptedString(string $key, mixed $value) Cast the given attribute to an encrypted string. |
|
static void | encryptUsing(Encrypter|null $encrypter) Set the encrypter instance that will be used to encrypt attributes. |
|
string | castAttributeAsHashedString(string $key, mixed $value) Cast the given attribute to a hashed string. |
|
mixed | fromFloat(mixed $value) Decode the given float. |
|
string | asDecimal(float|string $value, int $decimals) Return a decimal as string. |
|
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|null | fromDateTime(mixed $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 | isDateCastableWithCustomFormat(string $key) Determine whether a value is Date / DateTime custom-castable for inbound manipulation. |
|
bool | isJsonCastable(string $key) Determine whether a value is JSON castable for inbound manipulation. |
|
bool | isEncryptedCastable(string $key) Determine whether a value is an encrypted castable for inbound manipulation. |
|
bool | isClassCastable(string $key) Determine if the given key is cast using a custom class. |
|
bool | isEnumCastable(string $key) Determine if the given key is cast using an enum. |
|
bool | isClassDeviable(string $key) Determine if the key is deviable using a custom class. |
|
bool | isClassSerializable(string $key) Determine if the key is serializable using a custom class. |
|
mixed | resolveCasterClass(string $key) Resolve the custom caster class for a given key. |
|
string | parseCasterClass(string $class) Parse the given caster class, removing any arguments. |
|
void | mergeAttributesFromCachedCasts() Merge the cast class and attribute cast attributes back into the model. |
|
void | mergeAttributesFromClassCasts() Merge the cast class attributes back into the model. |
|
void | mergeAttributesFromAttributeCasts() Merge the cast class attributes back into the model. |
|
array | normalizeCastClassResponse(string $key, mixed $value) Normalize the response from a custom class caster. |
|
array | getAttributes() Get all of the current attributes on the model. |
|
array | getAttributesForInsert() Get all of the current attributes on the model for an insert operation. |
|
$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. |
|
mixed|array | getOriginalWithoutRewindingModel(string|null $key = null, mixed $default = null) Get the model's original attribute values. |
|
mixed|array | getRawOriginal(string|null $key = null, mixed $default = null) Get the model's raw 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 | syncOriginalAttributes(array|string $attributes) Sync multiple original attribute with their current values. |
|
$this | syncChanges() Sync the changed attributes. |
|
bool | isDirty(array|string|null $attributes = null) Determine if the model or any of the given attribute(s) have been modified. |
|
bool | isClean(array|string|null $attributes = null) Determine if the model or all the given attribute(s) have remained the same. |
|
$this | discardChanges() Discard attribute changes and reset the attributes to their original state. |
|
bool | wasChanged(array|string|null $attributes = null) Determine if the model or any of the given attribute(s) were changed when the model was last saved. |
|
bool | hasChanges(array $changes, array|string|null $attributes = null) Determine if any of the given attributes were changed when the model was last saved. |
|
array | getDirty() Get the attributes that have been changed since the last sync. |
|
array | getChanges() Get the attributes that were changed when the model was last saved. |
|
bool | originalIsEquivalent(string $key) Determine if the new and old values for a given key are equivalent. |
|
mixed | transformModelValue(string $key, mixed $value) Transform a raw model value using mutators, casts, etc. |
|
$this | append(array|string $attributes) Append attributes to query when building a query. |
|
array | getAppends() Get the accessors that are being appended to model arrays. |
|
$this | setAppends(array $appends) Set the accessors to append to model arrays. |
|
bool | hasAppended(string $attribute) Return whether the accessor attribute has been appended. |
|
array | getMutatedAttributes() Get the mutated attributes for a given instance. |
|
static void | cacheMutatedAttributes(object|string $classOrInstance) Extract and cache all the mutated attributes of a class. |
|
static array | getMutatorMethods(mixed $class) Get all of the attribute mutator methods. |
|
static array | getAttributeMarkedMutatorMethods(mixed $class) Get all of the "Attribute" return typed attribute mutator methods. |
Details
array attributesToArray()
Convert the model's attributes to an array.
protected array addDateAttributesToArray(array $attributes)
Add the date attributes to the attributes array.
protected array addMutatedAttributesToArray(array $attributes, array $mutatedAttributes)
Add the mutated attributes to the attributes array.
protected array addCastAttributesToArray(array $attributes, array $mutatedAttributes)
Add the casted attributes to the attributes array.
protected array getArrayableAttributes()
Get an attribute array of all arrayable attributes.
protected array getArrayableAppends()
Get all of the appendable values that are arrayable.
array relationsToArray()
Get the model's relationships in array form.
protected array getArrayableRelations()
Get an attribute array of all arrayable relations.
protected array getArrayableItems(array $values)
Get an attribute array of all arrayable values.
mixed getAttribute(string $key)
Get an attribute from the model.
protected null throwMissingAttributeExceptionIfApplicable(string $key)
Either throw a missing attribute exception or return null depending on Eloquent's configuration.
mixed getAttributeValue(string $key)
Get a plain attribute (not a relationship).
protected mixed getAttributeFromArray(string $key)
Get an attribute from the $attributes array.
mixed getRelationValue(string $key)
Get a relationship.
bool isRelation(string $key)
Determine if the given key is a relationship method on the model.
protected mixed handleLazyLoadingViolation(string $key)
Handle a lazy loading violation.
protected mixed getRelationshipFromMethod(string $method)
Get a relationship value from a method.
bool hasGetMutator(string $key)
Determine if a get mutator exists for an attribute.
bool hasAttributeMutator(string $key)
Determine if a "Attribute" return type marked mutator exists for an attribute.
bool hasAttributeGetMutator(string $key)
Determine if a "Attribute" return type marked get mutator exists for an attribute.
protected mixed mutateAttribute(string $key, mixed $value)
Get the value of an attribute using its mutator.
protected mixed mutateAttributeMarkedAttribute(string $key, mixed $value)
Get the value of an "Attribute" return type marked attribute using its mutator.
protected mixed mutateAttributeForArray(string $key, mixed $value)
Get the value of an attribute using its mutator for array conversion.
$this mergeCasts(array $casts)
Merge new casts with existing casts on the model.
protected mixed castAttribute(string $key, mixed $value)
Cast an attribute to a native PHP type.
protected mixed getClassCastableAttributeValue(string $key, mixed $value)
Cast the given attribute using a custom cast class.
protected mixed getEnumCastableAttributeValue(string $key, mixed $value)
Cast the given attribute to an enum.
protected string getCastType(string $key)
Get the type of cast for a model attribute.
protected mixed deviateClassCastableAttribute(string $method, string $key, mixed $value)
Increment or decrement the given attribute using the custom cast class.
protected mixed serializeClassCastableAttribute(string $key, mixed $value)
Serialize the given attribute using the custom cast class.
protected bool isCustomDateTimeCast(string $cast)
Determine if the cast type is a custom date time cast.
protected bool isImmutableCustomDateTimeCast(string $cast)
Determine if the cast type is an immutable custom date time cast.
protected bool isDecimalCast(string $cast)
Determine if the cast type is a decimal 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.
bool hasAttributeSetMutator(string $key)
Determine if an "Attribute" return type marked set mutator exists for an attribute.
protected mixed setMutatedAttributeValue(string $key, mixed $value)
Set the value of an attribute using its mutator.
protected mixed setAttributeMarkedMutatedAttributeValue(string $key, mixed $value)
Set the value of a "Attribute" return type marked attribute using its mutator.
protected 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.
protected void setClassCastableAttribute(string $key, mixed $value)
Set the value of a class castable attribute.
protected void setEnumCastableAttribute(string $key, UnitEnum|string|int $value)
Set the value of an enum castable attribute.
protected UnitEnum|BackedEnum getEnumCaseFromValue(string $enumClass, string|int $value)
Get an enum case instance from a given class and value.
protected string|int getStorableEnumValue(UnitEnum|BackedEnum $value)
Get the storable value from the given enum.
protected $this getArrayAttributeWithValue(string $path, string $key, mixed $value)
Get an array attribute with the given key and value set.
protected array getArrayAttributeByKey(string $key)
Get an array attribute or return an empty array if it is not set.
protected string castAttributeAsJson(string $key, mixed $value)
Cast the given attribute to JSON.
protected string asJson(mixed $value)
Encode the given value as JSON.
mixed fromJson(string $value, bool $asObject = false)
Decode the given JSON back into an array or object.
mixed fromEncryptedString(string $value)
Decrypt the given encrypted string.
protected string castAttributeAsEncryptedString(string $key, mixed $value)
Cast the given attribute to an encrypted string.
static void encryptUsing(Encrypter|null $encrypter)
Set the encrypter instance that will be used to encrypt attributes.
protected string castAttributeAsHashedString(string $key, mixed $value)
Cast the given attribute to a hashed string.
mixed fromFloat(mixed $value)
Decode the given float.
protected string asDecimal(float|string $value, int $decimals)
Return a decimal as string.
protected Carbon asDate(mixed $value)
Return a timestamp as DateTime object with time set to 00:00:00.
protected Carbon asDateTime(mixed $value)
Return a timestamp as DateTime object.
protected bool isStandardDateFormat(string $value)
Determine if the given value is a standard date format.
string|null fromDateTime(mixed $value)
Convert a DateTime to a storable string.
protected int asTimestamp(mixed $value)
Return a timestamp as unix timestamp.
protected 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.
protected bool isDateCastable(string $key)
Determine whether a value is Date / DateTime castable for inbound manipulation.
protected bool isDateCastableWithCustomFormat(string $key)
Determine whether a value is Date / DateTime custom-castable for inbound manipulation.
protected bool isJsonCastable(string $key)
Determine whether a value is JSON castable for inbound manipulation.
protected bool isEncryptedCastable(string $key)
Determine whether a value is an encrypted castable for inbound manipulation.
protected bool isClassCastable(string $key)
Determine if the given key is cast using a custom class.
protected bool isEnumCastable(string $key)
Determine if the given key is cast using an enum.
protected bool isClassDeviable(string $key)
Determine if the key is deviable using a custom class.
protected bool isClassSerializable(string $key)
Determine if the key is serializable using a custom class.
protected mixed resolveCasterClass(string $key)
Resolve the custom caster class for a given key.
protected string parseCasterClass(string $class)
Parse the given caster class, removing any arguments.
protected void mergeAttributesFromCachedCasts()
Merge the cast class and attribute cast attributes back into the model.
protected void mergeAttributesFromClassCasts()
Merge the cast class attributes back into the model.
protected void mergeAttributesFromAttributeCasts()
Merge the cast class attributes back into the model.
protected array normalizeCastClassResponse(string $key, mixed $value)
Normalize the response from a custom class caster.
array getAttributes()
Get all of the current attributes on the model.
protected array getAttributesForInsert()
Get all of the current attributes on the model for an insert operation.
$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.
protected mixed|array getOriginalWithoutRewindingModel(string|null $key = null, mixed $default = null)
Get the model's original attribute values.
mixed|array getRawOriginal(string|null $key = null, mixed $default = null)
Get the model's raw 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 syncOriginalAttributes(array|string $attributes)
Sync multiple original attribute with their current values.
$this syncChanges()
Sync the changed attributes.
bool isDirty(array|string|null $attributes = null)
Determine if the model or any of the given attribute(s) have been modified.
bool isClean(array|string|null $attributes = null)
Determine if the model or all the given attribute(s) have remained the same.
$this discardChanges()
Discard attribute changes and reset the attributes to their original state.
bool wasChanged(array|string|null $attributes = null)
Determine if the model or any of the given attribute(s) were changed when the model was last saved.
protected bool hasChanges(array $changes, array|string|null $attributes = null)
Determine if any of the given attributes were changed when the model was last saved.
array getDirty()
Get the attributes that have been changed since the last sync.
array getChanges()
Get the attributes that were changed when the model was last saved.
bool originalIsEquivalent(string $key)
Determine if the new and old values for a given key are equivalent.
protected mixed transformModelValue(string $key, mixed $value)
Transform a raw model value using mutators, casts, etc.
$this append(array|string $attributes)
Append attributes to query when building a query.
array getAppends()
Get the accessors that are being appended to model arrays.
$this setAppends(array $appends)
Set the accessors to append to model arrays.
bool hasAppended(string $attribute)
Return whether the accessor attribute has been appended.
array getMutatedAttributes()
Get the mutated attributes for a given instance.
static void cacheMutatedAttributes(object|string $classOrInstance)
Extract and cache all the mutated attributes of a class.
static protected array getMutatorMethods(mixed $class)
Get all of the attribute mutator methods.
static protected array getAttributeMarkedMutatorMethods(mixed $class)
Get all of the "Attribute" return typed attribute mutator methods.
© Taylor Otwell
Licensed under the MIT License.
Laravel is a trademark of Taylor Otwell.
https://laravel.com/api/10.x/Illuminate/Database/Eloquent/Concerns/HasAttributes.html