From 4ec00f8835233ea6dde42576d919dddcad64f236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Wed, 31 May 2023 16:41:45 +0200 Subject: [PATCH] Proposal for nullalbe translatable field --- app/Traits/Models/HasTranslations.php | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/Traits/Models/HasTranslations.php b/app/Traits/Models/HasTranslations.php index 2141b32a..b1b9e5dd 100644 --- a/app/Traits/Models/HasTranslations.php +++ b/app/Traits/Models/HasTranslations.php @@ -67,6 +67,42 @@ trait HasTranslations return parent::mutateAttributeForArray($key, $value); } + + /** + * Filter transalations to fetch + * + * ! Fix of spatie translatable. + * + * @param mixed $value + * @param string|null $locale + * @param array|null $allowedLocales + * @return boolean + */ + protected function filterTranslations( + mixed $value = null, + string $locale = null, + array $allowedLocales = null + ): bool { + if ($value === null) { + // ! Overload this to allow nullable. + return true; + } + + if ($value === '') { + return false; + } + + if ($allowedLocales === null) { + return true; + } + + if (!in_array($locale, $allowedLocales)) { + return false; + } + + return true; + } + /** * Retrieve the model for a bound value. *