Skip to content

Commit 720eaeb

Browse files
authored
Merge pull request #38 from Astrotomic/fix-issue-37
fix fill() conflicting attribute/locale
2 parents eab8e65 + 2b3bbed commit 720eaeb

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/Translatable/Translatable.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,19 @@ public function deleteTranslations($locales = null): void
9696
public function fill(array $attributes)
9797
{
9898
foreach ($attributes as $key => $values) {
99-
if ($this->getLocalesHelper()->has($key)) {
99+
if (
100+
$this->getLocalesHelper()->has($key)
101+
&& is_array($values)
102+
) {
100103
$this->getTranslationOrNew($key)->fill($values);
101104
unset($attributes[$key]);
102105
} else {
103106
[$attribute, $locale] = $this->getAttributeAndLocale($key);
104-
if ($this->isTranslationAttribute($attribute) and $this->getLocalesHelper()->has($locale)) {
107+
108+
if (
109+
$this->getLocalesHelper()->has($locale)
110+
&& $this->isTranslationAttribute($attribute)
111+
) {
105112
$this->getTranslationOrNew($locale)->fill([$attribute => $values]);
106113
unset($attributes[$key]);
107114
}

tests/TranslatableTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,4 +818,37 @@ public function test_translation_relation_not_found()
818818
$translation = Country::find(1)->translation;
819819
$this->assertNull($translation);
820820
}
821+
822+
public function test_can_fill_conflicting_attribute_locale()
823+
{
824+
config(['translatable.locales' => ['en', 'id']]);
825+
$this->app->make(\Astrotomic\Translatable\Locales::class)->load();
826+
827+
$city = new class extends \Astrotomic\Translatable\Test\Model\City {
828+
protected $guarded = [];
829+
protected $table = 'cities';
830+
public $translationModel = \Astrotomic\Translatable\Test\Model\CityTranslation::class;
831+
public $translationForeignKey = 'city_id';
832+
};
833+
834+
$city->fill([
835+
'country_id' => Country::first()->getKey(),
836+
'id' => [
837+
'name' => 'id:my city',
838+
],
839+
'en' => [
840+
'name' => 'en:my city',
841+
],
842+
]);
843+
844+
$city->fill([
845+
'id' => 100,
846+
]);
847+
848+
$city->save();
849+
850+
$this->assertEquals(100, $city->getKey());
851+
$this->assertEquals('id:my city', $city->getTranslation('id', false)->name);
852+
$this->assertEquals('en:my city', $city->getTranslation('en', false)->name);
853+
}
821854
}

0 commit comments

Comments
 (0)