Skip to content
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

[Bug] Translations don't work since spatie-translatable v6.10.0 #5770

Closed
invibe-sk opened this issue Feb 11, 2025 · 3 comments
Closed

[Bug] Translations don't work since spatie-translatable v6.10.0 #5770

invibe-sk opened this issue Feb 11, 2025 · 3 comments
Labels

Comments

@invibe-sk
Copy link

invibe-sk commented Feb 11, 2025

Bug report

Since version v6.10.0 of spatie/laravel-translatable package - translations don't work in backpack. Version 6.10 in spatie-translatable package introduced auto casting all translatable fields to array:

spatie/laravel-translatable@6.9.3...6.10.0

in file src/HasTranslations.php there were following lines added:

public function initializeHasTranslations(): void
{
    $this->mergeCasts(array_merge(
        $this->getCasts(),
        array_fill_keys($this->getTranslatableAttributes(), 'array'),
    ));
}

And therefore, decodeJsonCastedAttributes method inside src/app/Library/CrudPanel/Traits/Input.php file on line 216 strips the values from form when updating / creating records via Backpack admin panel.

What I did

Installed newest version of backpack with newest version of spatie-translatable

What I expected to happen

Translations to work properly inside backpack admin panel

What happened

They didn't. The DB record is always udated with null value ({"en": null} for example)

What I've already tried to fix it

Downgrading spatie/laravel-translatable to pre v6.10 version works as a temporary fix (v6.9.3 being latest before v6.10)

Is it a bug in the latest version of Backpack?

Yep

Backpack, Laravel, PHP, DB version

PHP VERSION:

8.3.15

PHP EXTENSIONS:

Core, date, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2, calendar, ctype, curl, dba, dom, hash, FFI, fileinfo, filter, ftp, gd, gettext, gmp, json, iconv, intl, SPL, ldap, mbstring, session, standard, odbc, pcntl, exif, mysqlnd, PDO, pdo_dblib, pdo_mysql, PDO_ODBC, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, pspell, random, readline, Reflection, mysqli, shmop, SimpleXML, soap, sockets, sodium, sysvmsg, sysvsem, sysvshm, tidy, tokenizer, xml, xmlreader, xmlwriter, xsl, zip, redis, imagick, Zend OPcache

LARAVEL VERSION:

11.37.0.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.3.6
backpack/crud: 6.7.51
backpack/generators: v4.0.7
backpack/pro: 2.2.32
backpack/theme-tabler: 1.2.17

Additional Information

Nothing special really, works as expected with older versions of spatie/laravel-translatable

Copy link

Based on the information provided in your bug report, it appears that there is indeed an incompatibility issue between the latest versions of Backpack and spatie/laravel-translatable (v6.10.0 and above). This issue is causing translations to not work properly in the Backpack admin panel. Let's break down the problem and the current workaround:

  1. The issue:
    The problem stems from a change introduced in spatie/laravel-translatable v6.10.0, where all translatable fields are automatically cast to arrays. This conflicts with Backpack's decodeJsonCastedAttributes method, resulting in null values being stored in the database for translations.

  2. Confirmation:
    This appears to be a bug in the latest version of Backpack when used with the latest version of spatie/laravel-translatable. The issue is not present in Backpack itself but arises from the interaction between the two packages.

  3. Temporary workaround:
    As you've discovered, downgrading spatie/laravel-translatable to version 6.9.3 (the latest version before 6.10.0) resolves the issue temporarily.

  4. Next steps:
    This issue needs to be addressed by the Backpack team to ensure compatibility with the latest version of spatie/laravel-translatable. They will likely need to update the decodeJsonCastedAttributes method or find another way to handle the new auto-casting behavior introduced by spatie/laravel-translatable.

I recommend reporting this issue on the Backpack GitHub repository if you haven't already done so. This will alert the maintainers to the problem and hopefully lead to a fix in a future release.

In the meantime, you can continue using the workaround of downgrading spatie/laravel-translatable to version 6.9.3 until an official fix is released.

Backpack CRUD GitHub Issues is the best place to report this bug and track its progress.

ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️

Relevant sources:

Github Issue (closed): [Bug] Regression with auto casting and spatie/laravel-translation
Github Issue (open): [v7] Remove spatie HasTranslations trait overwrite to allow compatibility with packages like spatie/laravel-tags
Github Issue (closed): [bug] translatable field in a relationship field display raw JSON
Github Issue (closed): [Bug] decodeJsonCastedAttributes sets field values to null
Github Issue (closed): [Bug] Translatable models using create method
Github Issue (closed): Translatable models and multi-language CRUDs form values not saved
Github Issue (closed): [Bug] translatable model not showing correct language translations in edit form
Github Issue (closed): [Bug] translatable models not showing correct locale
Github Issue (closed): [Bug] Translatable slugs not getting translated
Github Issue (closed): [Bug] Not translated uploder column in translatable model error on update
Github Issue (closed): FatalErrorException in CrudTrait.php line 224:
Github Issue (closed): display translatable field in select2_from_ajax

@dahdash
Copy link

dahdash commented Feb 11, 2025

🔍 Issue Confirmation

I can confirm this issue. Spatie’s HasTranslations automatically casts translatable fields as arrays, causing Backpack CRUD to store text fields incorrectly (e.g., {"en": null} instead of a string).

✅ Temporary Fix

To override this behavior, add the following method to your model:

/* Debug Spatie and Backpack conflict */
public function initializeHasTranslations(): void
{
    $this->mergeCasts(
        array_fill_keys($this->getTranslatableAttributes(), 'string'),
    );
}

This forces translatable fields to be treated as strings instead of arrays. Looking forward to a resolution! 🚀

@pxpm
Copy link
Contributor

pxpm commented Feb 13, 2025

This is now fixed in backpack/crud 6.7.53

Cheers

@pxpm pxpm closed this as completed Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

3 participants