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:
Ümit Tunç
2025-11-01 16:40:20 -03:00
parent 5fb74384ce
commit af0d7964ca
3 changed files with 27 additions and 57 deletions
@@ -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;
}