From 5e794fa2c2c58202721460e7a4abd3d1a52cadf1 Mon Sep 17 00:00:00 2001 From: Ludmil Simeonov Date: Tue, 14 Mar 2017 15:34:30 +0200 Subject: [PATCH] Transfer local for new instance creation of a model when translatable. This needs documentation --- src/Traits/HasTranslation.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Traits/HasTranslation.php b/src/Traits/HasTranslation.php index d43cbb6..85d1c97 100644 --- a/src/Traits/HasTranslation.php +++ b/src/Traits/HasTranslation.php @@ -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. @@ -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);