Skip to content

Commit 3dcd6aa

Browse files
author
Tihomir Banov
committed
Add optional where clause in scopeWithTranslations
1 parent aad5cc4 commit 3dcd6aa

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/Despark/LaravelDbLocalization/i18nModelTrait.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,21 @@ public function translate($locale = false)
110110
return $translation;
111111
}
112112

113-
public function scopeWithTranslations($query, $locale = 'en')
113+
public function scopeWithTranslations($query, $locale = null)
114114
{
115115
$i18nId = $this->getI18nId($locale);
116116
$translatorTable = new $this->translator();
117117
$translatorTableName = $translatorTable->getTable();
118118

119-
return $query->join(
120-
$translatorTableName,
121-
$translatorTableName.'.'.$this->getTranslatorField(), '=', $this->getTable().'.id',
122-
'left'
123-
)
124-
->where($translatorTableName.'.'.$this->getLocaleField(), '=', $i18nId);
119+
$query = $query->leftJoin(
120+
$translatorTableName,
121+
$translatorTableName.'.'.$this->getTranslatorField(), '=', $this->getTable().'.id');
122+
123+
if ($locale) {
124+
$query = $query->where($translatorTableName.'.'.$this->getLocaleField(), '=', $i18nId);
125+
}
126+
127+
return $query;
125128
}
126129

127130
/**

0 commit comments

Comments
 (0)