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, non-empty-list<int|string>|string> | $casts | The attributes that should be cast. |
|
| protected array | $classCastCache | The attributes that have been cast using custom classes. |
|
| protected array<string, array<string, mixed>> | $classCastStoredValues | The stored attributes each cached class cast was created from or last wrote. |
|
| protected array | $attributeCastCache | The attributes that have been cast using "Attribute" return type mutators. |
|
| protected array<string, array<string, mixed>> | $attributeCastStoredValues | The stored attributes each cached accessor was created from or last wrote. |
|
| 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 string[] | $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.
Ensure the decimal cast has a valid scale.
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.
Get the default attribute values for the model.
Merge the default values into the model's attributes.
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 whether a value is an encrypted class 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.
Merge cached cast values, advance their baseline, and indicate whether stored replacements took precedence.
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.
Wrap raw binary attribute bytes or streams for database binding.
Encode a binary attribute's cast value, such as a UUID string, for database binding.
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.
Compare JSON values using the structure exposed by their cast.
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 233
protected void
initializeHasAttributes()
Initialize the trait.
at line 262
array
attributesToArray()
Convert the model's attributes to an array.
at line 300
protected array
addDateAttributesToArray(array $attributes)
Add the date attributes to the attributes array.
at line 322
protected array
addMutatedAttributesToArray(array $attributes, array $mutatedAttributes)
Add the mutated attributes to the attributes array.
at line 351
protected array
addCastAttributesToArray(array $attributes, array $mutatedAttributes)
Add the casted attributes to the attributes array.
at line 405
protected array
getArrayableAttributes()
Get an attribute array of all arrayable attributes.
at line 413
protected array
getArrayableAppends()
Get all of the appendable values that are arrayable.
at line 427
array
relationsToArray()
Get the model's relationships in array form.
at line 469
protected array
getArrayableRelations()
Get an attribute array of all arrayable relations.
at line 477
protected array
getArrayableItems(array $values)
Get an attribute array of all arrayable values.
at line 493
bool
hasAttribute(string $key)
Determine whether an attribute exists on the model.
at line 509
mixed
getAttribute(string $key)
Get an attribute from the model.
at line 539
protected mixed
throwMissingAttributeExceptionIfApplicable(string $key)
Either throw a missing attribute exception or return null depending on Eloquent's configuration.
at line 560
mixed
getAttributeValue(string $key)
Get a plain attribute (not a relationship).
at line 568
protected mixed
getAttributeFromArray(string $key)
Get an attribute from the $attributes array.
at line 578
mixed
getRelationValue(string $key)
Get a relationship.
at line 608
bool
isRelation(string $key)
Determine if the given key is a relationship method on the model.
at line 623
protected mixed
handleLazyLoadingViolation(string $key)
Handle a lazy loading violation.
at line 648
protected mixed
getRelationshipFromMethod(string $method)
Get a relationship value from a method.
at line 676
bool
hasGetMutator(string $key)
Determine if a get mutator exists for an attribute.
at line 684
bool
hasAttributeMutator(string $key)
Determine if a "Attribute" return type marked mutator exists for an attribute.
at line 704
bool
hasAttributeGetMutator(string $key)
Determine if a "Attribute" return type marked get mutator exists for an attribute.
at line 720
bool
hasAnyGetMutator(string $key)
Determine if any get mutator exists for an attribute.
at line 728
protected mixed
mutateAttribute(string $key, mixed $value)
Get the value of an attribute using its mutator.
at line 738
protected mixed
mutateAttributeMarkedAttribute(string $key, mixed $value)
Get the value of an "Attribute" return type marked attribute using its mutator.
at line 765
protected mixed
mutateAttributeForArray(string $key, mixed $value)
Get the value of an attribute using its mutator for array conversion.
at line 790
HasAttributes
mergeCasts(array $casts)
Merge new casts with existing casts on the model.
at line 804
protected void
flushCastCaches()
Flush the per-instance cast metadata caches.
at line 815
protected array
ensureCastsAreStringValues(array $casts)
Ensure that the given casts are strings.
at line 847
protected mixed
castAttribute(string $key, mixed $value)
Cast an attribute to a native PHP type.
at line 915
protected mixed
getClassCastableAttributeValue(string $key, mixed $value)
Cast the given attribute using a custom cast class.
at line 945
protected mixed
getEnumCastableAttributeValue(string $key, mixed $value)
Cast the given attribute to an enum.
at line 963
protected string
getCastType(string $key)
Get the type of cast for a model attribute.
at line 997
protected mixed
deviateClassCastableAttribute(string $method, string $key, mixed $value)
Increment or decrement the given attribute using the custom cast class.
at line 1010
protected mixed
serializeClassCastableAttribute(string $key, mixed $value)
Serialize the given attribute using the custom cast class.
at line 1023
protected bool
compareClassCastableAttribute(string $key, mixed $original, mixed $value)
Compare two values for the given attribute using the custom cast class.
at line 1036
protected bool
isCustomDateTimeCast(string $cast)
Determine if the cast type is a custom date time cast.
at line 1045
protected bool
isImmutableCustomDateTimeCast(string $cast)
Determine if the cast type is an immutable custom date time cast.
at line 1054
protected bool
isDecimalCast(string $cast)
Determine if the cast type is a decimal cast.
at line 1062
protected void
ensureValidDecimalScale(string $cast, string $attribute)
Ensure the decimal cast has a valid scale.
at line 1076
mixed
setAttribute(string|int $key, mixed $value)
Set a given attribute on the model.
at line 1147
bool
hasSetMutator(string $key)
Determine if a set mutator exists for an attribute.
at line 1155
bool
hasAttributeSetMutator(string $key)
Determine if an "Attribute" return type marked set mutator exists for an attribute.
at line 1178
protected mixed
setMutatedAttributeValue(string $key, mixed $value)
Set the value of an attribute using its mutator.
at line 1194
protected mixed
setAttributeMarkedMutatedAttributeValue(string $key, mixed $value)
Set the value of a "Attribute" return type marked attribute using its mutator.
at line 1223
protected bool
isDateAttribute(string $key)
Determine if the given attribute is a date or date castable.
at line 1234
HasAttributes
fillJsonAttribute(string $key, mixed $value)
Set a given JSON attribute on the model.
at line 1264
protected void
setClassCastableAttribute(string $key, mixed $value)
Set the value of a class castable attribute.
at line 1293
protected void
setEnumCastableAttribute(string $key, mixed $value)
Set the value of an enum castable attribute.
at line 1316
protected mixed
getEnumCaseFromValue(string $enumClass, string|int $value)
Get an enum case instance from a given class and value.
at line 1330
protected string|int
getStorableEnumValue(string $expectedEnum, mixed $value)
Get the storable value from the given enum.
at line 1342
protected array
getArrayAttributeWithValue(string $path, string $key, mixed $value)
Get an array attribute with the given key and value set.
at line 1352
protected array
getArrayAttributeByKey(string $key)
Get an array attribute or return an empty array if it is not set.
at line 1370
protected string
castAttributeAsJson(string $key, mixed $value)
Cast the given attribute to JSON.
at line 1388
protected int
getJsonCastFlags(string $key)
Get the JSON casting flags for the given attribute.
at line 1402
protected string|false
asJson(mixed $value, int $flags = 0)
Encode the given value as JSON.
at line 1410
mixed
fromJson(string|int|float|null $value, bool $asObject = false)
Decode the given JSON back into an array or object.
at line 1422
mixed
fromEncryptedString(string $value)
Decrypt the given encrypted string.
at line 1430
protected string
castAttributeAsEncryptedString(string $key, mixed $value)
Cast the given attribute to an encrypted string.
at line 1441
static void
encryptUsing(Encrypter|null $encrypter)
Set the encrypter instance that will be used to encrypt attributes.
The encrypter persists in a static property for the worker lifetime and is used by primitive encrypted casts across all coroutines.
at line 1450
static Encrypter
currentEncrypter()
Get the current encrypter being used by the model.
at line 1460
protected string|null
castAttributeAsHashedString(string $key, mixed $value)
Cast the given attribute to a hashed string.
at line 1480
float
fromFloat(mixed $value)
Decode the given float.
at line 1500
protected string
asDecimal(float|int|string $value, int $decimals)
Return a decimal as string.
at line 1520
protected CarbonInterface
asDate(mixed $value)
Return a timestamp as DateTime object with time set to 00:00:00.
at line 1528
protected CarbonInterface
asDateTime(mixed $value)
Return a timestamp as DateTime object.
at line 1578
protected bool
isStandardDateFormat(string $value)
Determine if the given value is a standard date format.
at line 1586
string|null
fromDateTime(mixed $value)
Convert a DateTime to a storable string.
at line 1596
protected int
asTimestamp(mixed $value)
Return a timestamp as unix timestamp.
at line 1604
protected string
serializeDate(DateTimeInterface $date)
Prepare a date for array / JSON serialization.
at line 1617
array
getDates()
Get the attributes that should be converted to dates.
at line 1628
string
getDateFormat()
Get the format for database stored dates.
at line 1638
HasAttributes
setDateFormat(string $format)
Set the date format used by the model.
at line 1648
bool
hasCast(string $key, array|string|null $types = null)
Determine whether an attribute should be cast to a native type.
at line 1662
array
getCasts()
Get the attributes that should be cast.
at line 1679
protected array
casts()
Get the attributes that should be cast.
at line 1689
protected array
defaults()
Get the default attribute values for the model.
at line 1697
protected void
mergeDefaultAttributes()
Merge the default values into the model's attributes.
at line 1705
protected bool
isDateCastable(string $key)
Determine whether a value is Date / DateTime castable for inbound manipulation.
at line 1713
protected bool
isDateCastableWithCustomFormat(string $key)
Determine whether a value is Date / DateTime custom-castable for inbound manipulation.
at line 1721
protected bool
isJsonCastable(string $key)
Determine whether a value is JSON castable for inbound manipulation.
at line 1729
protected bool
isEncryptedCastable(string $key)
Determine whether a value is an encrypted castable for inbound manipulation.
at line 1737
protected bool
isEncryptedClassCastable(string $key)
Determine whether a value is an encrypted class castable for inbound manipulation.
at line 1748
protected bool
isClassCastable(string $key)
Determine if the given key is cast using a custom class.
at line 1778
protected bool
isEnumCastable(string $key)
Determine if the given key is cast using an enum.
at line 1808
protected bool
isClassDeviable(string $key)
Determine if the key is deviable using a custom class.
at line 1828
protected bool
isClassSerializable(string $key)
Determine if the key is serializable using a custom class.
at line 1838
protected bool
isClassComparable(string $key)
Determine if the key is comparable using a custom class.
at line 1848
protected mixed
resolveCasterClass(string $key)
Resolve the custom caster class for a given key.
at line 1884
static void
flushCasterCache()
Flush the caster cache for the model.
Clears the worker-wide caster instance cache shared by every coroutine; next attribute access rebuilds the casters.
at line 1892
protected string
parseCasterClass(string $class)
Parse the given caster class, removing any arguments.
at line 1902
protected void
mergeAttributesFromCachedCasts()
Merge the cast class and attribute cast attributes back into the model.
at line 1911
protected void
mergeAttributeFromCachedCasts(string $key)
Merge the cast class and attribute cast attribute back into the model.
at line 1920
protected void
mergeAttributesFromClassCasts()
Merge the cast class attributes back into the model.
at line 1930
protected void
mergeAttributeFromClassCasts(string $key)
Merge the cast class attribute back into the model.
at line 1951
protected void
mergeAttributesFromAttributeCasts()
Merge the cast class attributes back into the model.
at line 1961
protected void
mergeAttributeFromAttributeCasts(string $key)
Merge the cast class attribute back into the model.
at line 1995
protected bool
mergeCachedCastAttributes(array $attributes, array $storedValues)
Merge cached cast values, advance their baseline, and indicate whether stored replacements took precedence.
at line 2027
protected array
normalizeCastClassResponse(string $key, mixed $value)
Normalize the response from a custom class caster.
at line 2037
array
getAttributes()
Get all of the current attributes on the model.
at line 2047
protected array
getAttributesForInsert()
Get all of the current attributes on the model for an insert operation.
at line 2067
array
prepareBinaryAttributesForDatabase(array $attributes)
Wrap raw binary attribute bytes or streams for database binding.
at line 2091
mixed
encodeBinaryAttributeForDatabase(string $key, mixed $value)
Encode a binary attribute's cast value, such as a UUID string, for database binding.
Preserve explicit binary parameters and raw streams without encoding them again.
at line 2111
HasAttributes
setRawAttributes(array $attributes, bool $sync = false)
Set the array of model attributes. No checking is done.
at line 2133
mixed
getOriginal(string|null $key = null, mixed $default = null)
Get the model's original attribute values.
at line 2146
protected mixed
getOriginalWithoutRewindingModel(string|null $key = null, mixed $default = null)
Get the model's original attribute values.
at line 2165
mixed
getRawOriginal(string|null $key = null, mixed $default = null)
Get the model's raw original attribute values.
at line 2176
array
only(mixed $attributes)
Get a subset of the model's attributes.
at line 2192
array
except(mixed $attributes)
Get all attributes except the given ones.
at line 2212
HasAttributes
syncOriginal()
Sync the original attributes with the current.
at line 2224
HasAttributes
syncOriginalAttribute(string $attribute)
Sync a single original attribute with its current value.
at line 2235
HasAttributes
syncOriginalAttributes(array|string $attributes)
Sync multiple original attribute with their current values.
at line 2253
HasAttributes
syncChanges()
Sync the changed attributes.
at line 2287
bool
isDirty(array|string|null $attributes = null)
Determine if the model or any of the given attribute(s) have been modified.
at line 2300
bool
isClean(array|string|null $attributes = null)
Determine if the model or all the given attribute(s) have remained the same.
at line 2310
HasAttributes
discardChanges()
Discard attribute changes and reset the attributes to their original state.
at line 2327
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 2341
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 2367
array
getDirty()
Get the attributes that have been changed since the last sync.
at line 2399
protected array
getDirtyForUpdate()
Get the attributes that have been changed since the last sync for an update operation.
at line 2409
array
getChanges()
Get the attributes that were changed when the model was last saved.
at line 2419
array
getPrevious()
Get the attributes that were previously original before the model was last saved.
at line 2427
bool
originalIsEquivalent(string $key)
Determine if the new and old values for a given key are equivalent.
at line 2518
protected bool
jsonIsEquivalent(string|int|float|null $attribute, string|int|float|null $original, bool $asObject)
Compare JSON values using the structure exposed by their cast.
at line 2535
protected mixed
transformModelValue(string $key, mixed $value)
Transform a raw model value using mutators, casts, etc.
at line 2578
HasAttributes
append(array|string $attributes)
Append attributes to query when building a query.
at line 2592
array
getAppends()
Get the accessors that are being appended to model arrays.
at line 2603
HasAttributes
setAppends(array $appends)
Set the accessors to append to model arrays.
at line 2616
HasAttributes
mergeAppends(array $appends)
Merge new appended attributes with existing appended attributes on the model.
at line 2630
bool
hasAppended(string $attribute)
Return whether the accessor attribute has been appended.
at line 2640
HasAttributes
withoutAppends()
Remove all appended properties from the model.
at line 2650
array
getMutatedAttributes()
Get the mutated attributes for a given instance.
at line 2662
static void
cacheMutatedAttributes(object|string $classOrInstance)
Extract and cache all the mutated attributes of a class.
at line 2681
static protected array
getMutatorMethods(mixed $class)
Get all of the attribute mutator methods.
at line 2691
static protected array
getAttributeMarkedMutatorMethods(mixed $class)
Get all of the "Attribute" return typed attribute mutator methods.