From 5caff1d85dcabe569424b1d5d9ee4e73e3c377e5 Mon Sep 17 00:00:00 2001 From: Koen Hoeijmakers Date: Tue, 5 Jun 2018 11:27:26 +0200 Subject: [PATCH 1/2] purge translations when in "eloquent.deleting" --- src/HasTranslations.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/HasTranslations.php b/src/HasTranslations.php index 86f1926..6b77578 100644 --- a/src/HasTranslations.php +++ b/src/HasTranslations.php @@ -41,6 +41,10 @@ public static function bootHasTranslations() }); } + static::deleting(function (self $model) { + $model->purgeTranslations(); + }); + static::addGlobalScope(new JoinTranslationScope()); } @@ -63,6 +67,26 @@ public function translationExists(string $locale): bool return $this->translations()->where($this->getLocaleKeyName(), $locale)->exists(); } + /** + * Get all the translations. + * + * @return \Illuminate\Database\Eloquent\Collection + */ + public function getTranslations() + { + return $this->translations()->get(); + } + + /** + * Purge the translations. + * + * @return mixed + */ + public function purgeTranslations() + { + return $this->getTranslations()->each->delete(); + } + /** * Get the translation model. * From 52925cd7a5857fbc686e07ef6fd9bdfa7348a502 Mon Sep 17 00:00:00 2001 From: Sven Luijten Date: Tue, 5 Jun 2018 12:52:13 +0200 Subject: [PATCH 2/2] one delete query for purging all translations --- src/HasTranslations.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/HasTranslations.php b/src/HasTranslations.php index 6b77578..9add3df 100644 --- a/src/HasTranslations.php +++ b/src/HasTranslations.php @@ -67,16 +67,6 @@ public function translationExists(string $locale): bool return $this->translations()->where($this->getLocaleKeyName(), $locale)->exists(); } - /** - * Get all the translations. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getTranslations() - { - return $this->translations()->get(); - } - /** * Purge the translations. * @@ -84,7 +74,7 @@ public function getTranslations() */ public function purgeTranslations() { - return $this->getTranslations()->each->delete(); + return $this->translations()->delete(); } /**