Skip to content

Commit

Permalink
after successful save all translations - fresh "translation" relation…
Browse files Browse the repository at this point in the history
… if loaded
  • Loading branch information
p-andrey committed Mar 17, 2021
1 parent f3ab1cf commit 44799e1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ protected function saveTranslations(): bool
}
}

// Fresh "translation" relation if loaded
if ($saved && $this->relationLoaded('translation')) {
$this->load('translation');
}

return $saved;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,41 @@ public function it_update_all_translated_locales_when_translation_relation_is_lo
static::assertEquals('New Germany', $country->getTranslation('en', false)->name);
}

/** @test */
public function it_fresh_translation_relation_after_successful_save_translations()
{
$this->app->make('config')->set('translatable.locales', ['de', 'en']);
$this->app->setLocale('de');
$this->app->make(Locales::class)->load();

// First create country
CountryWithEarlyLoad::create([
'id' => 100,
'code' => 'my',
'de' => [
'name' => 'Deutschland',
],
'en' => [
'name' => 'Germany',
],
]);

$country = CountryWithEarlyLoad::find(100);

// try mass update
$country->update([
'code' => 'my',
'de' => [
'name' => 'New Deutschland',
],
'en' => [
'name' => 'New Germany',
],
]);

static::assertEquals('New Deutschland', $country->translation->name);
}

/** @test */
public function it_uses_translations_relation_if_locale_does_not_match(): void
{
Expand Down

0 comments on commit 44799e1

Please sign in to comment.