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();