Eloquent Custom Casts fire database queries on each save() #32808
Unanswered
aymericauberger
asked this question in
General
Replies: 2 comments
-
I'm going to convert this to a discussion since it is a bit of a feature request atm instead of a "bug". |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks! I wasn't sure where to put this, as it isn't a bug but I was expecting it would just work like the other cast types. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description:
When using an Eloquent custom cast on a model, if the value has been retrieved but not changed and we hit the
save()
method, a database query will be fired to re-insert the value and update the timestamps.I believe this is because the
originalIsEquivalent
method on theIlluminate\Database\Eloquent\Concerns\HasAttributes
trait does not support custom casts and will always return false.Steps To Reproduce:
Assuming we have a custom cast on the
address
property of theUser
model:Possible solution:
We could add a method to be implemented on the cast class, for instance
isEquivalent($attribute, $original)
, to determine if the value was changed or not.Beta Was this translation helpful? Give feedback.
All reactions