Skip to content

Commit

Permalink
Merge pull request #413 from pyrou/patch-1
Browse files Browse the repository at this point in the history
Accept $locale in withTranslation scope
  • Loading branch information
Gummibeer authored Jun 24, 2024
2 parents 0f6f7e7 + d99aab5 commit 250f9b3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Translatable/Traits/Scopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,20 @@ public function scopeWhereTranslationLike(Builder $query, string $translationFie
return $this->scopeWhereTranslation($query, $translationField, $value, $locale, 'whereHas', 'LIKE');
}

public function scopeWithTranslation(Builder $query)
public function scopeWithTranslation(Builder $query, ?string $locale = null)
{
$locale = $locale ?: $this->locale();

$query->with([
'translations' => function (Relation $query) {
'translations' => function (Relation $query) use ($locale) {
if ($this->useFallback()) {
$locale = $this->locale();
$countryFallbackLocale = $this->getFallbackLocale($locale); // e.g. de-DE => de
$locales = array_unique([$locale, $countryFallbackLocale, $this->getFallbackLocale()]);

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

return $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale());
return $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale);
},
]);
}
Expand Down

0 comments on commit 250f9b3

Please sign in to comment.