Skip to content

Commit 2bf4dcd

Browse files
alipadronfreekmurze
authored andcommitted
Handle null values in database columns as null in translations
- Updated the HasTranslations trait to return null for translations when the underlying database attribute is null. - Adjusted the logic in getTranslation to check if the raw attribute value is null before attempting to resolve translations. - Modified a test case to validate that null database values are treated as null in translations instead of empty strings.
1 parent 4509548 commit 2bf4dcd

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

src/HasTranslations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function getTranslation(string $key, string $locale, bool $useFallbackLoc
8282

8383
$translations = $this->getTranslations($key);
8484

85-
$translation = $translations[$normalizedLocale] ?? '';
85+
$translation = is_null(self::getAttributeFromArray($key)) ? null : $translations[$normalizedLocale] ?? '';
8686

8787
$translatableConfig = app(Translatable::class);
8888

tests/TranslatableTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ public function setAttributesExternally(array $attributes)
854854
[['en' => 'english', 'nl' => 'dutch'], ['en', 'nl'], ['english', 'dutch']],
855855
]);
856856

857-
it('should return empty string when the underlying attribute in database is null', function () {
857+
it('should return null when the underlying attribute in database is null', function () {
858858
// we need to remove the name attribute from the translatable array
859859
// and add it back to make sure the name
860860
// attribute is holding `null` raw value
@@ -864,7 +864,7 @@ public function setAttributesExternally(array $attributes)
864864

865865
$translation = $this->testModel->getTranslation('name', 'en');
866866

867-
expect($translation)->toBe('');
867+
expect($translation)->toBeNull();
868868
});
869869

870870
it('should return locales with empty string translations when allowEmptyStringForTranslation is true', function () {

0 commit comments

Comments
 (0)