Skip to content

Commit

Permalink
Add support for clearing translations with an empty array
Browse files Browse the repository at this point in the history
Update HasTranslations trait to handle empty arrays as valid input for clearing translations.
Add a test to ensure empty arrays clear translations for the specified attribute.
  • Loading branch information
alipadron committed Jan 3, 2025
1 parent de81937 commit b75a9c7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function setAttribute($key, $value)
return parent::setAttribute($key, $value);
}

if (is_array($value) && ! array_is_list($value)) {
if (is_array($value) && (! array_is_list($value) || count($value) === 0)) {
return $this->setTranslations($key, $value);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,10 @@ public function setAttributesExternally(array $attributes)

expect($this->testModel->getTranslation('name', 'en'))->toEqual(['testValue_en']);
});

it('can treat an empty array as value for clearing translations', function () {
$this->testModel->name = [];
$this->testModel->save();

expect($this->testModel->getTranslations('name'))->toEqual([]);
});

0 comments on commit b75a9c7

Please sign in to comment.