Skip to content
This repository was archived by the owner on Jun 18, 2019. It is now read-only.

Commit 0980ddd

Browse files
authored
Merge pull request #567 from dimsav/issue-538
drop custom save method in favor of saved event listener
2 parents 485b33a + cde60b5 commit 0980ddd

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

src/Translatable/Translatable.php

+8-30
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ trait Translatable
1414

1515
protected $defaultLocale;
1616

17+
public static function bootTranslatable(): void
18+
{
19+
static::saved(function (Model $model) {
20+
/* @var Translatable $model */
21+
return $model->saveTranslations();
22+
});
23+
}
24+
1725
/**
1826
* Alias for getTranslation().
1927
*
@@ -245,36 +253,6 @@ public function setAttribute($key, $value)
245253
return $this;
246254
}
247255

248-
/**
249-
* @param array $options
250-
*
251-
* @return bool
252-
*/
253-
public function save(array $options = [])
254-
{
255-
if ($this->exists && ! $this->isDirty()) {
256-
// If $this->exists and not dirty, parent::save() skips saving and returns
257-
// false. So we have to save the translations
258-
if ($this->fireModelEvent('saving') === false) {
259-
return false;
260-
}
261-
262-
if ($saved = $this->saveTranslations()) {
263-
$this->fireModelEvent('saved', false);
264-
$this->fireModelEvent('updated', false);
265-
}
266-
267-
return $saved;
268-
}
269-
270-
// We save the translations only if the instance is saved in the database.
271-
if (parent::save($options)) {
272-
return $this->saveTranslations();
273-
}
274-
275-
return false;
276-
}
277-
278256
/**
279257
* @param string $locale
280258
*

0 commit comments

Comments
 (0)