Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/Repositories/Traits/TranslationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down
Loading