-
-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
is it possible to set value to NULL if request value is empty ? #85
Comments
Hi, My code :
Don't forget to Hope this help. |
@ligne13 i have a solution for the controller protected function cleanEmptyTranslations($request)
{
$result = $request->all(); // use "except" instead if u want to exclude something out
foreach ($result as $k => $v) {
if (is_array($v)) {
$result[$k] = array_filter($v) ?: null;
}
}
return $result;
} |
@ctf0 yes, I've also written a similar method, but I put it in a Trait used by my models. In my controller, before calling public function setTranslationsFromRequest(Request $request)
{
foreach ($this->getTranslatableAttributes() as $attribute) {
if ($request->$attribute && ! empty($request->$attribute)) {
$translations = json_decode($request->$attribute, true);
foreach ($translations as $locale => $value) {
if ( ! empty($value)) {
$this->setTranslation($attribute, $locale, $value);
} else {
$this->forgetTranslation($attribute, $locale);
}
}
}
}
return $this;
} Too bad that the package does not offer an option for handling these empty strings. I tried to make a patch to the source code but didn't succeed... |
Feel free to PR a well tested PR to improve this behaviour. |
atm because the package works according to the default locale,
so if the column had an empty value the package will save it as
{"en": ""}
or{"en": null}
.however, is there a way to save it as
NULL
instead ?EDIT
maybe a solution would be forgetting the local if the key value is empty ?
The text was updated successfully, but these errors were encountered: