Skip to content

Commit d99aab5

Browse files
authored
Accept $locale in withTranslation scope
1 parent 0f6f7e7 commit d99aab5

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/Translatable/Traits/Scopes.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,20 @@ public function scopeWhereTranslationLike(Builder $query, string $translationFie
106106
return $this->scopeWhereTranslation($query, $translationField, $value, $locale, 'whereHas', 'LIKE');
107107
}
108108

109-
public function scopeWithTranslation(Builder $query)
109+
public function scopeWithTranslation(Builder $query, ?string $locale = null)
110110
{
111+
$locale = $locale ?: $this->locale();
112+
111113
$query->with([
112-
'translations' => function (Relation $query) {
114+
'translations' => function (Relation $query) use ($locale) {
113115
if ($this->useFallback()) {
114-
$locale = $this->locale();
115116
$countryFallbackLocale = $this->getFallbackLocale($locale); // e.g. de-DE => de
116117
$locales = array_unique([$locale, $countryFallbackLocale, $this->getFallbackLocale()]);
117118

118119
return $query->whereIn($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locales);
119120
}
120121

121-
return $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale());
122+
return $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale);
122123
},
123124
]);
124125
}

0 commit comments

Comments
 (0)