Skip to content

Commit 5e794fa

Browse files
committed
Transfer local for new instance creation of a model when translatable. This needs documentation
1 parent 0d4610c commit 5e794fa

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/Traits/HasTranslation.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ public function translation()
4848
return $this->hasOne(TranslationModel::class, 'parent_id', 'id')->where('locale', \App::getLocale());
4949
}
5050

51+
/**
52+
* Create a new instance of the given model.
53+
*
54+
* @param array $attributes
55+
* @param bool $exists
56+
* @return static
57+
*/
58+
public function newInstance($attributes = [], $exists = false)
59+
{
60+
$model = parent::newInstance($attributes, $exists);
61+
$model->setActiveLocale($this->getActiveLocale());
62+
63+
return $model;
64+
}
5165

5266
/**
5367
* Create a new model instance for a related model.
@@ -144,17 +158,19 @@ public function unsetTranslation($locale, $key = null)
144158
$value = null;
145159
}
146160
}
147-
148161
}
149162

150163
/**
151164
* @param $key
152165
* @param $value
166+
* @return $this
153167
*/
154168
public function setAttribute($key, $value)
155169
{
156170
if ($this->isTranslatable($key)) {
157171
$this->setTranslation($key, $this->getActiveLocale(), $value);
172+
173+
return $this;
158174
}
159175

160176
return parent::setAttribute($key, $value);

0 commit comments

Comments
 (0)