spatie / laravel-translatable

Making Eloquent models translatable
https://spatie.be/docs/laravel-translatable
MIT License
2.22k stars 277 forks source link

Incorrect saving of empty translation values to the database #456

Open a1ex7 opened 2 weeks ago

a1ex7 commented 2 weeks ago

✏️ Describe the bug

When saving translations, some of which have an empty value (null or empty string), only the last value is written to the database.

↪️ To Reproduce

Execute

$promotion->replaceTranslations('name', [
    "uk" => "Діти від 10 до 15 років",
    "ru" => "Дети от 10 до 15 лет",
    "en" => "Children from 10 to 15 years old",
    "pl" => "Podziewać od 10 do 15 lat",
    "de" => null,
    "cs" => null,
    "fr" => null,
    "es" => null,
    "it" => null,
]);

$promotion->save();

Then execute

$promotion->getTranslations('name');

The result will be correct image

But in the value saved in the database we see the following

$promotion->getOriginal('name');

image

✅ Expected behavior

[
    "uk" => "Діти від 10 до 15 років",
    "ru" => "Дети от 10 до 15 лет",
    "en" => "Children from 10 to 15 years old",
    "pl" => "Podziewać od 10 do 15 lat",
    "de" => null,
    "cs" => null,
    "fr" => null,
    "es" => null,
    "it" => null,
]

or

[
    "uk" => "Діти від 10 до 15 років",
    "ru" => "Дети от 10 до 15 лет",
    "en" => "Children from 10 to 15 years old",
    "pl" => "Podziewać od 10 do 15 lat",
]
freekmurze commented 2 weeks ago

Could you submit a PR to fix this?