Skip to content
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

Closed
ctf0 opened this issue Oct 20, 2017 · 4 comments
Closed

is it possible to set value to NULL if request value is empty ? #85

ctf0 opened this issue Oct 20, 2017 · 4 comments

Comments

@ctf0
Copy link
Contributor

ctf0 commented Oct 20, 2017

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 ?

@ligne13
Copy link

ligne13 commented Apr 17, 2018

Hi,
I also needed to consider empty strings as non existing translations. I handled this with an Observer watching the saving event on my model, which parses the translations on the model and forget the ones with empty values.

My code :

<?php

namespace App\Observers;

use Illuminate\Database\Eloquent\Model;

class TranslatableObserver
{
    /**
     * Removes empty strings in translations before saving model
     *
     * @param Model $modelInstance
     */
    public function saving(Model $modelInstance)
    {
        $translations = $modelInstance->getTranslations();
        foreach ($translations as $key => $values) {
            foreach ($values as $locale => $value) {
                if (empty($value)) {
                    $modelInstance->forgetTranslation($key, $locale);
                }
            }
        }
    }
}

Don't forget to observe your models in your AppService Provider, example :
Post::observe(TranslatableObserver::class);

Hope this help.

@ctf0
Copy link
Contributor Author

ctf0 commented Apr 17, 2018

@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;
    }

@ligne13
Copy link

ligne13 commented Apr 18, 2018

@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 save(), I call something like $post->setTranslationsFromRequest($request);

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...

@freekmurze
Copy link
Member

Feel free to PR a well tested PR to improve this behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants