Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salipro4ever/f769f10206cffaf56c2533c574b9022b to your computer and use it in GitHub Desktop.
Save salipro4ever/f769f10206cffaf56c2533c574b9022b to your computer and use it in GitHub Desktop.
Allow show translation attribute on both table (dimsav/laravel-translatable)

Only override this method on Model

/**
     * @param array $attributes
     *
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
     * @return $this
     */
public function fill(array $attributes)
{
    foreach ($attributes as $key => $values) {
        if ($this->isKeyALocale($key)) {
            $this->getTranslationOrNew($key)->fill($values);
            unset($attributes[$key]);
        } else {
            list($attribute, $locale) = $this->getAttributeAndLocale($key);
            if ($this->isTranslationAttribute($attribute) and $this->isKeyALocale($locale)) {
                $this->getTranslationOrNew($locale)->fill([$attribute => $values]);
                unset($attributes[$key]); // <- change here
            }
        }
    }

    return parent::fill($attributes);
}

To

...
$this->getTranslationOrNew($locale)->fill([$attribute => $values]);
if(!in_array($key, $this->fillable)) {
    unset($attributes[$key]);
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment