From c2ac417060c98952e0c098ad379a92c70447b4a3 Mon Sep 17 00:00:00 2001 From: celikerde Date: Thu, 9 Apr 2026 18:58:47 +0300 Subject: [PATCH] fix(TranslationsTrait): add afterSaveTranslationsTrait method for timestamp management - Introduced afterSaveTranslationsTrait method to re-enable timestamps and touch the parent model only when meaningful changes occur in translation rows, ignoring auto-timestamp columns. This enhances the handling of translation updates in the model. --- src/Repositories/Traits/TranslationsTrait.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Repositories/Traits/TranslationsTrait.php b/src/Repositories/Traits/TranslationsTrait.php index 0edf79866..36595a1bb 100755 --- a/src/Repositories/Traits/TranslationsTrait.php +++ b/src/Repositories/Traits/TranslationsTrait.php @@ -78,7 +78,7 @@ public function prepareFieldsBeforeSaveTranslationsTrait($object, $fields) $activeField = $shouldPublishFirstLanguage || (isset($submittedLanguage) ? $submittedLanguage['published'] : false); $fields[$locale] = [ - 'active' => $activeField, + 'active' => (int) $activeField, ] + $attributes->mapWithKeys(function ($attribute) use (&$fields, $locale, $localesCount, $index, $translationsFields) { $attributeValue = $fields[$attribute] ?? $translationsFields[$attribute] ?? null; @@ -205,6 +205,36 @@ public function orderTranslationsTrait($query, &$orders) } } + /** + * After save, re-enable timestamps and touch the parent model + * only when a translation row really changed (ignoring auto-timestamp + * columns that the translation table may carry). + * + * @param \Illuminate\Database\Eloquent\Model $object + * @param array $fields + * @return void + */ + public function afterSaveTranslationsTrait($object, $fields) + { + if (! $this->model->isTranslatable()) { + return; + } + + if ($object->relationLoaded('translations')) { + $timestampKeys = ['updated_at', 'created_at', 'deleted_at']; + + foreach ($object->translations as $translation) { + $changedKeys = array_keys($translation->getChanges()); + $meaningfulChanges = array_diff($changedKeys, $timestampKeys); + + if (! empty($meaningfulChanges)) { + $this->letEloquentModelBeTouched(true); + break; + } + } + } + } + /** * @return array */