Skip to content

Commit

Permalink
Transfer local for new instance creation of a model when translatable…
Browse files Browse the repository at this point in the history
…. This needs documentation
  • Loading branch information
lsimeonov committed Mar 14, 2017
1 parent 0d4610c commit 5e794fa
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Traits/HasTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public function translation()
return $this->hasOne(TranslationModel::class, 'parent_id', 'id')->where('locale', \App::getLocale());
}

/**
* Create a new instance of the given model.
*
* @param array $attributes
* @param bool $exists
* @return static
*/
public function newInstance($attributes = [], $exists = false)
{
$model = parent::newInstance($attributes, $exists);
$model->setActiveLocale($this->getActiveLocale());

return $model;
}

/**
* Create a new model instance for a related model.
Expand Down Expand Up @@ -144,17 +158,19 @@ public function unsetTranslation($locale, $key = null)
$value = null;
}
}

}

/**
* @param $key
* @param $value
* @return $this
*/
public function setAttribute($key, $value)
{
if ($this->isTranslatable($key)) {
$this->setTranslation($key, $this->getActiveLocale(), $value);

return $this;
}

return parent::setAttribute($key, $value);
Expand Down

0 comments on commit 5e794fa

Please sign in to comment.