Replies: 1 comment
-
Moving this to discussions. |
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
-
I really appreciate the new automatic casting when using
getOrignal
, but I think with these changes some unexpected behavior was introduced.I know there have been some related issues here and there, but I think they do not exactly cover my point.
First consider an attribute accessor like the following
In previous laravel versions a call like
$model->getOriginal('foo)
would just have returnednull
, because this attributes is not present in the original attributes array.However in current versions of laravel it will search and find the accessor, call it and return
"foo"
.One could argue that this expected behavior, because the accessor returns a constant value, so it has obviously not changed in the meantime.
But consider what happens, when you define an accessor like the full name attribute example from the docs:
When I now call
$model->getOriginal('full_name)
I will always get the name created from the current instead of the original state. And as far as I understand the new mechansim does not offer any neat means to solve this problem, because it would only pass the original value to the function if its key matches with the custom attribute name.So my simple suggestion would be to generally throw an exception if one tries to call
getOriginal
with a key that is not present in the original array to avoid calculating with wrong values without noticing.Beta Was this translation helpful? Give feedback.
All reactions