From 08bb358aa2b7a8eebbbb4563dfafb1550cfe2249 Mon Sep 17 00:00:00 2001 From: Gilles Felix Date: Fri, 8 Sep 2023 10:31:28 +0200 Subject: [PATCH] Add scopeWithLocale --- README.md | 3 ++- src/Traits/Translatable.php | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c55e1cb..871a8d2 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ This trait add : * A relation `translations` containing all translations of the model * A relation `translationsWithDeleted` containing all translations of the model, including those in trash if your model use SoftDelete trait * A function `translate(string $locale, array $translateAttributes = [])` to translate a model in a new locale -* A function `getTranslation(string $locale, bool $withDeleted = false)` returning the translated model in specified locale or null if it doesn't exist. +* A function `getTranslation(string $locale, bool $withDeleted = false)` returning the translated model in specified locale or null if it doesn't exist. +* A scope `withLocale($locale)` on query ```php $post = new Post([ diff --git a/src/Traits/Translatable.php b/src/Traits/Translatable.php index d30d615..90e9d0d 100644 --- a/src/Traits/Translatable.php +++ b/src/Traits/Translatable.php @@ -2,6 +2,7 @@ namespace Novius\LaravelTranslatable\Traits; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletingScope; @@ -77,6 +78,11 @@ public function translationsWithDeleted() return $this->translations(); } + public function scopeWithLocale(Builder $query, ?string $locale): Builder + { + return $query->where('locale', $locale); + } + public function translate(string $locale, array $translateAttributes = []): static { $localeColumn = $this->getLocaleColumn();