From 6783217994f71c1bddb681d41811aa8f1c17f7de Mon Sep 17 00:00:00 2001 From: Sebastian Hilger Date: Thu, 11 Feb 2021 12:20:35 +0100 Subject: [PATCH] fix error on create --- src/TranslatableFieldMixin.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/TranslatableFieldMixin.php b/src/TranslatableFieldMixin.php index 4a2da7f..24528ee 100644 --- a/src/TranslatableFieldMixin.php +++ b/src/TranslatableFieldMixin.php @@ -102,9 +102,20 @@ public function translatable() $this->fillUsing(function ($request, $model, $attribute, $requestAttribute) use ($locales) { $realAttribute = FieldServiceProvider::normalizeAttribute($this->meta['translatable']['original_attribute'] ?? $attribute); $translations = $request->{$realAttribute}; - foreach ($locales as $localeKey => $localeName) { - $model->translate($localeKey)->{$realAttribute} = $translations[$localeKey]; + + if($request->editMode == 'create') { + + foreach ($locales as $localeKey => $localeName) { + $translationEntry = $model->translateOrNew($localeKey); + + $translationEntry->{$realAttribute} = $translations[$localeKey]; + } + } else { + foreach ($locales as $localeKey => $localeName) { + $model->translate($localeKey)->{$realAttribute} = $translations[$localeKey]; + } } + }); return $this;