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

Commit

Permalink
Merge pull request #567 from dimsav/issue-538
Browse files Browse the repository at this point in the history
drop custom save method in favor of saved event listener
  • Loading branch information
Gummibeer committed Jun 3, 2019
2 parents 485b33a + cde60b5 commit 0980ddd
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions src/Translatable/Translatable.php
Expand Up @@ -14,6 +14,14 @@ trait Translatable

protected $defaultLocale;

public static function bootTranslatable(): void
{
static::saved(function (Model $model) {
/* @var Translatable $model */
return $model->saveTranslations();
});
}

/**
* Alias for getTranslation().
*
Expand Down Expand Up @@ -245,36 +253,6 @@ public function setAttribute($key, $value)
return $this;
}

/**
* @param array $options
*
* @return bool
*/
public function save(array $options = [])
{
if ($this->exists && ! $this->isDirty()) {
// If $this->exists and not dirty, parent::save() skips saving and returns
// false. So we have to save the translations
if ($this->fireModelEvent('saving') === false) {
return false;
}

if ($saved = $this->saveTranslations()) {
$this->fireModelEvent('saved', false);
$this->fireModelEvent('updated', false);
}

return $saved;
}

// We save the translations only if the instance is saved in the database.
if (parent::save($options)) {
return $this->saveTranslations();
}

return false;
}

/**
* @param string $locale
*
Expand Down

0 comments on commit 0980ddd

Please sign in to comment.