[Bug] translatable model not showing correct language translations in edit form
See original GitHub issueBug report
What I did
Tried to use HasTranslations trait on my MenuItem model in order to be able to use translations for that model
What I expected to happen
To be able to translate my models in different languages
What happened
Every language edit form shows same current values (of default language). Save operation works - for example, I can change values and it shows different in in database, but backpack edit form still shows values from default language for every language.
What I’ve already tried to fix it
Tried to override/extend these methods: vendor/backpack/crud/src/app/Http/Controllers/Operations/UpdateOperation.php -> edit vendor/backpack/crud/src/app/Library/CrudPanel/Traits/Update.php -> getModelAttributeValue But the final result gets ugly and/or with a lot of repetitive code. I believe the issue is 80 line of Update.php -> getModelAttributeValue:
return $model->{$field['name']};
it takes default value of the model (default locale) whereas instead it should take translated value for corresponding locale that’s in request (_locale)
quick fix in UpdateOperation.php -> edit:
app()->setLocale(request()->input('_locale') ?? app()->getLocale());
$this->crud->setOperationSetting('fields', $this->crud->getUpdateFields());
app()->setLocale(config('app.locale'));
Is it a bug in the latest version of Backpack? - yes
After I run composer update backpack/crud
the bug… is it still there? - yes
Backpack, Laravel, PHP, DB version
When I run php artisan backpack:version
the output is:
### PHP VERSION:
PHP 8.0.6 (cli) (built: May 4 2021 23:31:45) ( ZTS Visual C++ 2019 x64 )
Copyright (c) The PHP Group
Zend Engine v4.0.6, Copyright (c) Zend Technologies
### LARAVEL VERSION:
v9.9.0@4d5a07640891b772188d7737348886a0222737d8
### BACKPACK VERSION:
5.0.14@1e355c4c046a34423a1a3e3150120245a4bfd8e9
Issue Analytics
- State:
- Created a year ago
- Comments:14 (12 by maintainers)
Top GitHub Comments
Oh, got it! Meant to tag @kartomantas then sorry. In any case…
We’ve just merged https://github.com/Laravel-Backpack/CRUD/pull/4399 and tagged 5.0.19 which fixes this issue. Please run
composer update backpack/crud
to get 5.0.19 or higher, and this should be fixed! 🎉 If it’s not, please reply here to let us know, and we’ll reopen.Sorry again this took so long to fix. We mistakenly thought this was about a more minor problem.
Cheers!
@tabacitu hi this is not my topic, I closed (https://github.com/Laravel-Backpack/CRUD/issues/4326) mine when I saw the fix from @pxpm https://github.com/Laravel-Backpack/CRUD/pull/4267
and I’m already using two of his PRs (very important to me), so it’s still hard for me to update to new versions 😀(second https://github.com/Laravel-Backpack/CRUD/pull/4359)
but yes, in the current topic the problem is that the language does not change