HasAttributes
trait HasAttributes
Properties
| protected array<string, mixed> | $attributes | The model's attributes. |
|
| protected array<string, mixed> | $original | The model attribute's original state. |
|
| protected array<string, mixed> | $changes | The changed model attributes. |
|
| protected array<string, mixed> | $previous | The previous state of the changed model attributes. |
|
| protected array<string, string> | $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. |
|
| protected array|null | $mergedCastsCache | The cached merged casts array for this instance. |
|
| protected array | $castMetadataCache | The cached results of cast metadata predicate methods for this instance. |
|
| static protected string[] | $primitiveCastTypes | The built-in, primitive cast types supported by Eloquent. |
|
| protected string|null | $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 | $casterCache | The cache of the resolved custom caster class instances. |
|
| 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
Initialize the trait.
Convert the model's attributes to an array.
Add the date attributes to the attributes array.
Add the mutated attributes to the attributes array.
Add the casted attributes to the attributes array.
Get an attribute array of all arrayable attributes.
Get all of the appendable values that are arrayable.
Get the model's relationships in array form.
Get an attribute array of all arrayable relations.
Get an attribute array of all arrayable values.
Determine whether an attribute exists on the model.
Get an attribute from the model.
Either throw a missing attribute exception or return null depending on Eloquent's configuration.
Get a plain attribute (not a relationship).
Get an attribute from the $attributes array.
Get a relationship.
Determine if the given key is a relationship method on the model.
Handle a lazy loading violation.
Get a relationship value from a method.
Determine if a get mutator exists for an attribute.
Determine if a "Attribute" return type marked mutator exists for an attribute.
Determine if a "Attribute" return type marked get mutator exists for an attribute.
Determine if any get mutator exists for an attribute.
Get the value of an attribute using its mutator.
Get the value of an "Attribute" return type marked attribute using its mutator.
Get the value of an attribute using its mutator for array conversion.
Merge new casts with existing casts on the model.
Flush the per-instance cast metadata caches.
Ensure that the given casts are strings.
Cast an attribute to a native PHP type.
Cast the given attribute using a custom cast class.
Cast the given attribute to an enum.
Get the type of cast for a model attribute.
Increment or decrement the given attribute using the custom cast class.
Serialize the given attribute using the custom cast class.
Compare two values for the given attribute using the custom cast class.
Determine if the cast type is a custom date time cast.
Determine if the cast type is an immutable custom date time cast.
Determine if the cast type is a decimal cast.
Set a given attribute on the model.
Determine if a set mutator exists for an attribute.
Determine if an "Attribute" return type marked set mutator exists for an attribute.
Set the value of an attribute using its mutator.
Set the value of a "Attribute" return type marked attribute using its mutator.
Determine if the given attribute is a date or date castable.
Set a given JSON attribute on the model.
Set the value of a class castable attribute.
Set the value of an enum castable attribute.
Get an enum case instance from a given class and value.
Get the storable value from the given enum.
Get an array attribute with the given key and value set.
Get an array attribute or return an empty array if it is not set.
Cast the given attribute to JSON.
Get the JSON casting flags for the given attribute.
Encode the given value as JSON.
Decode the given JSON back into an array or object.
Decrypt the given encrypted string.
Cast the given attribute to an encrypted string.
Set the encrypter instance that will be used to encrypt attributes.
Get the current encrypter being used by the model.
Cast the given attribute to a hashed string.
Decode the given float.
Return a decimal as string.
Return a timestamp as DateTime object with time set to 00:00:00.
Return a timestamp as DateTime object.
Determine if the given value is a standard date format.
Convert a DateTime to a storable string.
Return a timestamp as unix timestamp.
Get the attributes that should be converted to dates.
Get the format for database stored dates.
Set the date format used by the model.
Determine whether an attribute should be cast to a native type.
Get the attributes that should be cast.
Get the attributes that should be cast.
Determine whether a value is Date / DateTime castable for inbound manipulation.
Determine whether a value is Date / DateTime custom-castable for inbound manipulation.
Determine whether a value is JSON castable for inbound manipulation.
Determine whether a value is an encrypted castable for inbound manipulation.
Determine if the given key is cast using a custom class.
Determine if the given key is cast using an enum.
Determine if the key is deviable using a custom class.
Determine if the key is serializable using a custom class.
Determine if the key is comparable using a custom class.
Resolve the custom caster class for a given key.
Flush the caster cache for the model.
Parse the given caster class, removing any arguments.
Merge the cast class and attribute cast attributes back into the model.
Merge the cast class and attribute cast attribute back into the model.
Merge the cast class attributes back into the model.
Merge the cast class attribute back into the model.
Merge the cast class attributes back into the model.
Merge the cast class attribute back into the model.
Normalize the response from a custom class caster.
Get all of the current attributes on the model.
Get all of the current attributes on the model for an insert operation.
Set the array of model attributes. No checking is done.
Get the model's original attribute values.
Get the model's original attribute values.
Get the model's raw original attribute values.
Get a subset of the model's attributes.
Get all attributes except the given ones.
Sync the original attributes with the current.
Sync a single original attribute with its current value.
Sync multiple original attribute with their current values.
Sync the changed attributes.
Determine if the model or any of the given attribute(s) have been modified.
Determine if the model or all the given attribute(s) have remained the same.
Discard attribute changes and reset the attributes to their original state.
Determine if the model or any of the given attribute(s) were changed when the model was last saved.
Determine if any of the given attributes were changed when the model was last saved.
Get the attributes that have been changed since the last sync.
Get the attributes that have been changed since the last sync for an update operation.
Get the attributes that were changed when the model was last saved.
Get the attributes that were previously original before the model was last saved.
Determine if the new and old values for a given key are equivalent.
Transform a raw model value using mutators, casts, etc.
Append attributes to query when building a query.
Get the accessors that are being appended to model arrays.
Set the accessors to append to model arrays.
Merge new appended attributes with existing appended attributes on the model.
Return whether the accessor attribute has been appended.
Remove all appended properties from the model.
Get the mutated attributes for a given instance.
Extract and cache all the mutated attributes of a class.
Get all of the attribute mutator methods.
Get all of the "Attribute" return typed attribute mutator methods.
Details
at line 208
protected void
initializeHasAttributes()
Initialize the trait.
at line 233
array
attributesToArray()
Convert the model's attributes to an array.
at line 271
protected array
addDateAttributesToArray(array $attributes)
Add the date attributes to the attributes array.
at line 293
protected array
addMutatedAttributesToArray(array $attributes, array $mutatedAttributes)
Add the mutated attributes to the attributes array.
at line 322
protected array
addCastAttributesToArray(array $attributes, array $mutatedAttributes)
Add the casted attributes to the attributes array.
at line 376
protected array
getArrayableAttributes()
Get an attribute array of all arrayable attributes.
at line 384
protected array
getArrayableAppends()
Get all of the appendable values that are arrayable.
at line 398
array
relationsToArray()
Get the model's relationships in array form.
at line 440
protected array
getArrayableRelations()
Get an attribute array of all arrayable relations.
at line 448
protected array
getArrayableItems(array $values)
Get an attribute array of all arrayable values.
at line 464
bool
hasAttribute(string $key)
Determine whether an attribute exists on the model.
at line 480
mixed
getAttribute(string $key)
Get an attribute from the model.
at line 510
protected mixed
throwMissingAttributeExceptionIfApplicable(string $key)
Either throw a missing attribute exception or return null depending on Eloquent's configuration.
at line 528
mixed
getAttributeValue(string $key)
Get a plain attribute (not a relationship).
at line 536
protected mixed
getAttributeFromArray(string $key)
Get an attribute from the $attributes array.
at line 546
mixed
getRelationValue(string $key)
Get a relationship.
at line 576
bool
isRelation(string $key)
Determine if the given key is a relationship method on the model.
at line 589
protected mixed
handleLazyLoadingViolation(string $key)
Handle a lazy loading violation.
at line 607
protected mixed
getRelationshipFromMethod(string $method)
Get a relationship value from a method.
at line 635
bool
hasGetMutator(string $key)
Determine if a get mutator exists for an attribute.
at line 643
bool
hasAttributeMutator(string $key)
Determine if a "Attribute" return type marked mutator exists for an attribute.
at line 663
bool
hasAttributeGetMutator(string $key)
Determine if a "Attribute" return type marked get mutator exists for an attribute.
at line 679
bool
hasAnyGetMutator(string $key)
Determine if any get mutator exists for an attribute.
at line 687
protected mixed
mutateAttribute(string $key, mixed $value)
Get the value of an attribute using its mutator.
at line 697
protected mixed
mutateAttributeMarkedAttribute(string $key, mixed $value)
Get the value of an "Attribute" return type marked attribute using its mutator.
at line 723
protected mixed
mutateAttributeForArray(string $key, mixed $value)
Get the value of an attribute using its mutator for array conversion.
at line 744
HasAttributes
mergeCasts(array $casts)
Merge new casts with existing casts on the model.
at line 758
protected void
flushCastCaches()
Flush the per-instance cast metadata caches.
at line 767
protected array
ensureCastsAreStringValues(array $casts)
Ensure that the given casts are strings.
at line 799
protected mixed
castAttribute(string $key, mixed $value)
Cast an attribute to a native PHP type.
at line 867
protected mixed
getClassCastableAttributeValue(string $key, mixed $value)
Cast the given attribute using a custom cast class.
at line 894
protected mixed
getEnumCastableAttributeValue(string $key, mixed $value)
Cast the given attribute to an enum.
at line 912
protected string
getCastType(string $key)
Get the type of cast for a model attribute.
at line 942
protected mixed
deviateClassCastableAttribute(string $method, string $key, mixed $value)
Increment or decrement the given attribute using the custom cast class.
at line 955
protected mixed
serializeClassCastableAttribute(string $key, mixed $value)
Serialize the given attribute using the custom cast class.
at line 968
protected bool
compareClassCastableAttribute(string $key, mixed $original, mixed $value)
Compare two values for the given attribute using the custom cast class.
at line 981
protected bool
isCustomDateTimeCast(string $cast)
Determine if the cast type is a custom date time cast.
at line 990
protected bool
isImmutableCustomDateTimeCast(string $cast)
Determine if the cast type is an immutable custom date time cast.
at line 999
protected bool
isDecimalCast(string $cast)
Determine if the cast type is a decimal cast.
at line 1007
mixed
setAttribute(string|int $key, mixed $value)
Set a given attribute on the model.
at line 1076
bool
hasSetMutator(string $key)
Determine if a set mutator exists for an attribute.
at line 1084
bool
hasAttributeSetMutator(string $key)
Determine if an "Attribute" return type marked set mutator exists for an attribute.
at line 1107
protected mixed
setMutatedAttributeValue(string $key, mixed $value)
Set the value of an attribute using its mutator.
at line 1117
protected mixed
setAttributeMarkedMutatedAttributeValue(string $key, mixed $value)
Set the value of a "Attribute" return type marked attribute using its mutator.
at line 1147
protected bool
isDateAttribute(string $key)
Determine if the given attribute is a date or date castable.
at line 1156
HasAttributes
fillJsonAttribute(string $key, mixed $value)
Set a given JSON attribute on the model.
at line 1180
protected void
setClassCastableAttribute(string $key, mixed $value)
Set the value of a class castable attribute.
at line 1208
protected void
setEnumCastableAttribute(string $key, mixed $value)
Set the value of an enum castable attribute.
at line 1229
protected mixed
getEnumCaseFromValue(string $enumClass, string|int $value)
Get an enum case instance from a given class and value.
at line 1241
protected string|int
getStorableEnumValue(string $expectedEnum, mixed $value)
Get the storable value from the given enum.
at line 1253
protected array
getArrayAttributeWithValue(string $path, string $key, mixed $value)
Get an array attribute with the given key and value set.
at line 1263
protected array
getArrayAttributeByKey(string $key)
Get an array attribute or return an empty array if it is not set.
at line 1279
protected string
castAttributeAsJson(string $key, mixed $value)
Cast the given attribute to JSON.
at line 1297
protected int
getJsonCastFlags(string $key)
Get the JSON casting flags for the given attribute.
at line 1311
protected string|false
asJson(mixed $value, int $flags = 0)
Encode the given value as JSON.
at line 1319
mixed
fromJson(string|null $value, bool $asObject = false)
Decode the given JSON back into an array or object.
at line 1331
mixed
fromEncryptedString(string $value)
Decrypt the given encrypted string.
at line 1339
protected string
castAttributeAsEncryptedString(string $key, mixed $value)
Cast the given attribute to an encrypted string.
at line 1350
static void
encryptUsing(Encrypter|null $encrypter)
Set the encrypter instance that will be used to encrypt attributes.
Boot-only. The encrypter persists in a static property for the worker lifetime and is used by every encrypted attribute across all coroutines.
at line 1358
static Encrypter
currentEncrypter()
Get the current encrypter being used by the model.
at line 1366
protected string|null
castAttributeAsHashedString(string $key, mixed $value)
Cast the given attribute to a hashed string.
at line 1387
float
fromFloat(mixed $value)
Decode the given float.
at line 1400
protected string
asDecimal(float|string $value, int $decimals)
Return a decimal as string.
at line 1412
protected CarbonInterface
asDate(mixed $value)
Return a timestamp as DateTime object with time set to 00:00:00.
at line 1420
protected CarbonInterface
asDateTime(mixed $value)
Return a timestamp as DateTime object.
at line 1471
protected bool
isStandardDateFormat(string $value)
Determine if the given value is a standard date format.
at line 1479
string|null
fromDateTime(mixed $value)
Convert a DateTime to a storable string.
at line 1489
protected int
asTimestamp(mixed $value)
Return a timestamp as unix timestamp.
at line 1497
protected string
serializeDate(DateTimeInterface $date)
Prepare a date for array / JSON serialization.
at line 1509
array
getDates()
Get the attributes that should be converted to dates.
at line 1520
string
getDateFormat()
Get the format for database stored dates.
at line 1528
HasAttributes
setDateFormat(string $format)
Set the date format used by the model.
at line 1538
bool
hasCast(string $key, array|string|null $types = null)
Determine whether an attribute should be cast to a native type.
at line 1550
array
getCasts()
Get the attributes that should be cast.
at line 1567
protected array
casts()
Get the attributes that should be cast.
at line 1575
protected bool
isDateCastable(string $key)
Determine whether a value is Date / DateTime castable for inbound manipulation.
at line 1583
protected bool
isDateCastableWithCustomFormat(string $key)
Determine whether a value is Date / DateTime custom-castable for inbound manipulation.
at line 1591
protected bool
isJsonCastable(string $key)
Determine whether a value is JSON castable for inbound manipulation.
at line 1599
protected bool
isEncryptedCastable(string $key)
Determine whether a value is an encrypted castable for inbound manipulation.
at line 1609
protected bool
isClassCastable(string $key)
Determine if the given key is cast using a custom class.
at line 1637
protected bool
isEnumCastable(string $key)
Determine if the given key is cast using an enum.
at line 1667
protected bool
isClassDeviable(string $key)
Determine if the key is deviable using a custom class.
at line 1687
protected bool
isClassSerializable(string $key)
Determine if the key is serializable using a custom class.
at line 1697
protected bool
isClassComparable(string $key)
Determine if the key is comparable using a custom class.
at line 1707
protected mixed
resolveCasterClass(string $key)
Resolve the custom caster class for a given key.
at line 1743
static void
flushCasterCache()
Flush the caster cache for the model.
Boot or tests only. Clears the worker-wide caster instance cache shared by every coroutine; next attribute access rebuilds the casters.
at line 1751
protected string
parseCasterClass(string $class)
Parse the given caster class, removing any arguments.
at line 1761
protected void
mergeAttributesFromCachedCasts()
Merge the cast class and attribute cast attributes back into the model.
at line 1770
protected void
mergeAttributeFromCachedCasts(string $key)
Merge the cast class and attribute cast attribute back into the model.
at line 1779
protected void
mergeAttributesFromClassCasts()
Merge the cast class attributes back into the model.
at line 1789
protected void
mergeAttributeFromClassCasts(string $key)
Merge the cast class attribute back into the model.
at line 1809
protected void
mergeAttributesFromAttributeCasts()
Merge the cast class attributes back into the model.
at line 1819
protected void
mergeAttributeFromAttributeCasts(string $key)
Merge the cast class attribute back into the model.
at line 1848
protected array
normalizeCastClassResponse(string $key, mixed $value)
Normalize the response from a custom class caster.
at line 1858
array
getAttributes()
Get all of the current attributes on the model.
at line 1868
protected array
getAttributesForInsert()
Get all of the current attributes on the model for an insert operation.
at line 1876
HasAttributes
setRawAttributes(array $attributes, bool $sync = false)
Set the array of model attributes. No checking is done.
at line 1895
mixed
getOriginal(string|null $key = null, mixed $default = null)
Get the model's original attribute values.
at line 1908
protected mixed
getOriginalWithoutRewindingModel(string|null $key = null, mixed $default = null)
Get the model's original attribute values.
at line 1927
mixed
getRawOriginal(string|null $key = null, mixed $default = null)
Get the model's raw original attribute values.
at line 1938
array
only(mixed $attributes)
Get a subset of the model's attributes.
at line 1954
array
except(mixed $attributes)
Get all attributes except the given ones.
at line 1972
HasAttributes
syncOriginal()
Sync the original attributes with the current.
at line 1982
HasAttributes
syncOriginalAttribute(string $attribute)
Sync a single original attribute with its current value.
at line 1992
HasAttributes
syncOriginalAttributes(array|string $attributes)
Sync multiple original attribute with their current values.
at line 2008
HasAttributes
syncChanges()
Sync the changed attributes.
at line 2021
bool
isDirty(array|string|null $attributes = null)
Determine if the model or any of the given attribute(s) have been modified.
at line 2034
bool
isClean(array|string|null $attributes = null)
Determine if the model or all the given attribute(s) have remained the same.
at line 2042
HasAttributes
discardChanges()
Discard attribute changes and reset the attributes to their original state.
at line 2057
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.
at line 2071
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.
at line 2097
array
getDirty()
Get the attributes that have been changed since the last sync.
at line 2115
protected array
getDirtyForUpdate()
Get the attributes that have been changed since the last sync for an update operation.
at line 2125
array
getChanges()
Get the attributes that were changed when the model was last saved.
at line 2135
array
getPrevious()
Get the attributes that were previously original before the model was last saved.
at line 2143
bool
originalIsEquivalent(string $key)
Determine if the new and old values for a given key are equivalent.
at line 2204
protected mixed
transformModelValue(string $key, mixed $value)
Transform a raw model value using mutators, casts, etc.
at line 2246
HasAttributes
append(array|string $attributes)
Append attributes to query when building a query.
at line 2258
array
getAppends()
Get the accessors that are being appended to model arrays.
at line 2266
HasAttributes
setAppends(array $appends)
Set the accessors to append to model arrays.
at line 2278
HasAttributes
mergeAppends(array $appends)
Merge new appended attributes with existing appended attributes on the model.
at line 2292
bool
hasAppended(string $attribute)
Return whether the accessor attribute has been appended.
at line 2300
HasAttributes
withoutAppends()
Remove all appended properties from the model.
at line 2308
array
getMutatedAttributes()
Get the mutated attributes for a given instance.
at line 2320
static void
cacheMutatedAttributes(object|string $classOrInstance)
Extract and cache all the mutated attributes of a class.
at line 2339
static protected array
getMutatorMethods(mixed $class)
Get all of the attribute mutator methods.
at line 2349
static protected array
getAttributeMarkedMutatorMethods(mixed $class)
Get all of the "Attribute" return typed attribute mutator methods.