Accessors on translatable attributes no longer returning json values #445
Replies: 2 comments 2 replies
-
https://github.com/spatie/laravel-translatable/pull/437/files This PR and release also broke a lot of things on my side. For example, this code: public function getImportantNoticeAttribute()
{
if ($this->important_notice_expires?->isPast()) {
return null;
}
return $this->getTranslations('important_notice');
} Worked very well before release 6.6.2. Now it returns a double-nested value like this: I'm not sure of what issue the PR was supposed to fix, I only feel the things that it broke. The "fix" would be to apply it like so: public function getImportantNoticeAttribute($value)
{
if ($this->important_notice_expires?->isPast()) {
return null;
}
return $value;
} But the fact that it's looped through by some magical array in the background makes no sense to me and feels counter intuitive. Not to mention that it's a breaking change. It should definitely not be a minor release. What do you think @freekmurze? |
Beta Was this translation helpful? Give feedback.
-
Hi! I'm the author of the mentioned PR. I think the package works as expected by checking your examples. If you'd like to keep only the current locale translations in the array representation, the preferred way should be https://spatie.be/docs/laravel-translatable/v6/advanced-usage/customize-the-toarray-method like @goaround mentioned. @blackjak231 You could just delete the accessor altogether and customize the toArray method instead. @Tarpsvo In your case I think the implementation of the accessor is incorrect. It shouldn't return all the translations, using Let me know if I can help with anything else! |
Beta Was this translation helpful? Give feedback.
-
Hey !
I've been using a custom Trait mentioned here for 2 years now without any problems. To summarize, I have the following Trait with mutators :
So far,
$value
has always been equal to the json/array value of the attribute. However, since upgrading to Laravel 11, it seems the Attribute or event the Casting logic has changed and $value goes through each translation string in the array individually.Here is an example of what I mean :
Let's imaging a title attribute with the following value (in DB) :
and the following mutator
And a simple controller method :
On Laravel 10,
Log::info
would only log{'fr': 'Mon titre','en': 'My title'}
. If i don't log the value and us thegetTranslatedAttribute
method it returns only the translated title of the current Locale.On Laravel 11, it logs
Mon titre
and then another logMy title
. If i don't log the value and us thegetTranslatedAttribute
method it returns the full json.I've been trying to go deeper into this library and into the way Laravel casting/mutaitng works but I've reached my max understanding after a few hours of digging...
Is anyone able to reproduce this and able to help me wrap my head around this issue ? What changed and could anything be fixed in the package ?
Thanks !
FYI: I'm aware I could always do the following :
However I want to limit the use of static array keys.
Beta Was this translation helpful? Give feedback.
All reactions