Skip to content
This repository was archived by the owner on Jun 18, 2019. It is now read-only.

use fallback in attributesToArray #503

Merged
merged 6 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
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
22 changes: 13 additions & 9 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,23 @@ private function usePropertyFallback()
*/
private function getAttributeOrFallback($locale, $attribute)
{
$value = $this->getTranslation($locale)->$attribute;
$translation = $this->getTranslation($locale);

if (
empty($value) &&
$this->usePropertyFallback() &&
($fallback = $this->getTranslation($this->getFallbackLocale(), true))
(
! $translation instanceof Model ||
empty($translation->$attribute)
) &&
$this->usePropertyFallback()
) {
return $fallback->$attribute;
$translation = $this->getTranslation($this->getFallbackLocale(), true);
}

return $value;
if ($translation instanceof Model) {
return $translation->$attribute;
}

return null;
}

/**
Expand Down Expand Up @@ -695,9 +701,7 @@ public function attributesToArray()
continue;
}

if ($translations = $this->getTranslation()) {
$attributes[$field] = $translations->$field;
}
$attributes[$field] = $this->getAttributeOrFallback(null, $field);
}

return $attributes;
Expand Down
1 change: 1 addition & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ public function test_fallback_for_country_based_locales_with_no_base_locale()

public function test_to_array_and_fallback_with_country_based_locales_enabled()
{
$this->app->config->set('translatable.locale', 'en-GB');
$this->app->config->set('translatable.use_fallback', true);
$this->app->config->set('translatable.fallback_locale', 'fr');
$this->app->config->set('translatable.locales', ['en' => ['GB'], 'fr']);
Expand Down