Skip to content

Commit ee1a5fe

Browse files
author
Tihomir Banov
committed
Scope with translation return empty when there is no translation
1 parent 9fd3ff2 commit ee1a5fe

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/Despark/LaravelDbLocalization/i18nModelTrait.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,24 @@ public function scopeWithTranslations($query, $locale = null, $softDelete = null
115115
$i18nId = $this->getI18nId($locale);
116116
$translatorTable = new $this->translator();
117117
$translatorTableName = $translatorTable->getTable();
118-
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);
118+
$transfield = $this->getTranslatorField();
119+
$table = $this->getTable();
120+
$localeField = $this->getLocaleField();
121+
122+
if (! $locale) {
123+
$query = $query->leftJoin(
124+
$translatorTableName,
125+
$translatorTableName.'.'.$transfield, '=', $table.'.id');
126+
} else {
127+
$query = $query->leftJoin(\DB::raw(
128+
'( SELECT
129+
translatorAlias.*
130+
FROM '.$translatorTableName.' as translatorAlias
131+
WHERE translatorAlias.'.$localeField.' = '.$i18nId.'
132+
) as '.$translatorTableName
133+
), function ($join) use ($translatorTableName, $transfield, $table) {
134+
$join->on($translatorTableName.'.'.$transfield, '=', $table.'.id');
135+
});
125136
}
126137

127138
if ($softDelete) {
@@ -155,10 +166,6 @@ public function save(array $options = [])
155166
if (empty($options)) {
156167
$options = \Input::all();
157168
}
158-
// if method is put or patch remove translations
159-
if (\Request::method() === 'PUT' || \Request::method() === 'PATCH') {
160-
$this->deleteTranslations($this->id);
161-
}
162169

163170
$translatableId = $this->saveTranslatable($options);
164171
$this->saveTranslations($translatableId, $options);
@@ -212,22 +219,21 @@ public function saveTranslations($translatableId, $options)
212219
}
213220
}
214221
}
215-
$translationModel = new $this->translator();
216222

217-
foreach ($translationsArray as $translationValues) {
218-
$translationModel->create($translationValues);
219-
}
220-
}
223+
$modelName = $this->translator;
221224

222-
/**
223-
* Delete all translations.
224-
*
225-
* @param array translatable Id
226-
*/
227-
public function deleteTranslations($translatableId)
228-
{
229-
$translationModel = new $this->translator();
225+
foreach ($translationsArray as $translationValues) {
226+
$translation = $modelName::where($this->translatorField, array_get($translationValues, $this->translatorField))
227+
->where($this->localeField, array_get($translationValues, $this->localeField))
228+
->first();
230229

231-
$translationModel::where($this->translatorField, $translatableId)->delete();
230+
if (! isset($translation->id)) {
231+
$translation = new $modelName();
232+
}
233+
foreach ($translationValues as $key => $val) {
234+
$translation->$key = $val;
235+
}
236+
$translation->save();
237+
}
232238
}
233239
}

0 commit comments

Comments
 (0)