Enhance translation handling in EditPage and TranslationTabs: Implemented eager loading for translations to optimize performance and avoid N+1 query issues. Updated the EditPage mount method to load translations, and modified the fillFromRecord method in TranslationTabs to utilize eager-loaded translations. Disabled old format conversion in mutateFormDataBeforeFill to prevent memory exhaustion with nested arrays, ensuring data integrity while maintaining original format.
This commit is contained in:
@@ -167,10 +167,20 @@ class TranslationTabs
|
||||
$languages = Language::active()->ordered()->get();
|
||||
$fields = $record->getTranslatableFields();
|
||||
|
||||
// Eager load all translations to avoid N+1 queries
|
||||
$allTranslations = $record->translations()
|
||||
->whereIn('language_code', $languages->pluck('code'))
|
||||
->whereIn('field_name', $fields)
|
||||
->get()
|
||||
->groupBy('language_code');
|
||||
|
||||
foreach ($languages as $language) {
|
||||
$data['translations'][$language->code] = [];
|
||||
$languageTranslations = $allTranslations->get($language->code) ?? collect();
|
||||
$translationsByField = $languageTranslations->keyBy('field_name');
|
||||
|
||||
foreach ($fields as $field) {
|
||||
$translation = $record->getTranslation($field, $language->code, false);
|
||||
$translation = $translationsByField->get($field);
|
||||
if ($translation) {
|
||||
$data['translations'][$language->code][$field] = $translation->field_value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user