You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're using spatie/laravel-translatable together with spatie/laravel-data, and in most cases the auto-casting of translatable fields to the current locale (since v4.10 #870 ) works great. However, we have a use case where we need to retrieve all translations of a translatable field as an array, for example, when sending data to our backoffice to manage all locales in one view.
To achieve this, we experimented with a trait that adds a helper withAllTranslationsFrom() method to temporarily clear the $translatable property before passing the model to from():
This works fine for simple flat Data objects. However, it breaks down when dealing with nested data structures (e.g., nested Data objects or collections), as those nested objects still receive already-casted strings.
Has anyone dealt with this kind of issue before? Is there a recommended way to keep the full translation arrays available when mapping a model to a Data object? Or a way to configure laravel-data to avoid auto-casting translations at all in certain cases?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, thanks for the great package!
We're using
spatie/laravel-translatable
together withspatie/laravel-data
, and in most cases the auto-casting of translatable fields to the current locale (since v4.10 #870 ) works great. However, we have a use case where we need to retrieve all translations of a translatable field as an array, for example, when sending data to our backoffice to manage all locales in one view.To achieve this, we experimented with a trait that adds a helper
withAllTranslationsFrom()
method to temporarily clear the$translatable
property before passing the model tofrom()
:This works fine for simple flat Data objects. However, it breaks down when dealing with nested data structures (e.g., nested
Data
objects or collections), as those nested objects still receive already-casted strings.Has anyone dealt with this kind of issue before? Is there a recommended way to keep the full translation arrays available when mapping a model to a Data object? Or a way to configure
laravel-data
to avoid auto-casting translations at all in certain cases?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions