-
Notifications
You must be signed in to change notification settings - Fork 11.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom attribute accessor yields "column does not exist" error #40364
Comments
Having the same issue, accessors with the new syntax that are intended to be getter-only are sometimes trying to be saved to the database even though there's no underlying column for them. |
Can you try with the latest Laravel version? This has changed a bit. |
Is there any new version since yesterday? Because I reproduced this bug in
a fresh install yesterday, as shown in the linked repository.
…On Thu 13. Jan 2022 at 15:24 Dries Vints ***@***.***> wrote:
Can you try with the latest Laravel version? This has changed a bit.
—
Reply to this email directly, view it on GitHub
<#40364 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB5O3UQSSPNSE7FXMSUOEIDUV3OAZANCNFSM5LZAXX5Q>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Yes. Please try with Laravel v8.79.0 and verify that this version resolves it or not. |
Ah, indeed it seems to have been fixed in 29a6692. I'll go ahead and close the issue then. Thanks! |
Description:
With reference to the method
mutateAttributeMarkedAttribute
found in theHasAttributes
trait introduced with the new attribute casting feature in v8.77:It seems to me that the calculated
$value
is only cached if it's an object, which essentially means anycallable
Attribute that returns scalar values (string, boolean etc.) will not be cached. I'm not sure of the reason behind this, but even with this aside, it seems the logic here introduces a bug. Considering the methodmergeAttributesFromAttributeCasts()
in the same trait, which is called upon saving/updating a model:If I read it correctly, this method will merge
$attributeCastCache
back to the model before performing the query. Now if in the model I have an attribute accessor like this:Once
$user->foo
is accessed, a'foo'
key will be stored into$attributeCastCache
and merged into$attributes
. If my table doesn't have afoo
column, however, saving the model will yield an error saying the columnfoo
doesn't exist.This problem won't happen if the closure provided in
Attribute::get()
returns a non-object value, because'foo'
won't be registered into$attributeCastCache
and as such won't exist in the$attributes
array after merging.Steps to Reproduce:
composer install
php artisan test
. The test should break.More Information:
The bug (?) didn't exist with the old syntax i.e.
getFooAttribute()
. Of course, as a workaround, I can changefoo()
from an attribute into a method like this:But this a) makes it a bit less elegant (IMHO) and b) makes it harder to add custom attributes into
appends
.Of course, if this is just me not understanding Laravel, my apologies.
The text was updated successfully, but these errors were encountered: