Skip to content

Commit b62f4ab

Browse files
author
Tihomir Banov
committed
Add soft delete condition to scope with translation
1 parent ee1a5fe commit b62f4ab

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

src/Despark/LaravelDbLocalization/i18nModelTrait.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,35 @@ public function translate($locale = false)
112112

113113
public function scopeWithTranslations($query, $locale = null, $softDelete = null)
114114
{
115+
// get i18n id by locale
115116
$i18nId = $this->getI18nId($locale);
117+
116118
$translatorTable = new $this->translator();
117119
$translatorTableName = $translatorTable->getTable();
118-
$transfield = $this->getTranslatorField();
119-
$table = $this->getTable();
120+
$translatableTable = $this->getTable();
121+
122+
$translatorField = $this->getTranslatorField();
120123
$localeField = $this->getLocaleField();
121124

122125
if (! $locale) {
123126
$query = $query->leftJoin(
124127
$translatorTableName,
125-
$translatorTableName.'.'.$transfield, '=', $table.'.id');
128+
$translatorTableName.'.'.$translatorField, '=', $translatableTable.'.id');
126129
} else {
130+
$aliasSoftDelete = '';
131+
if ($softDelete) {
132+
$aliasSoftDelete = 'AND translatorAlias.deleted_at is null ';
133+
}
134+
127135
$query = $query->leftJoin(\DB::raw(
128136
'( SELECT
129137
translatorAlias.*
130138
FROM '.$translatorTableName.' as translatorAlias
131139
WHERE translatorAlias.'.$localeField.' = '.$i18nId.'
140+
'.$aliasSoftDelete.'
132141
) as '.$translatorTableName
133-
), function ($join) use ($translatorTableName, $transfield, $table) {
134-
$join->on($translatorTableName.'.'.$transfield, '=', $table.'.id');
142+
), function ($join) use ($translatorTableName, $translatorField, $translatableTable) {
143+
$join->on($translatorTableName.'.'.$translatorField, '=', $translatableTable.'.id');
135144
});
136145
}
137146

@@ -142,20 +151,6 @@ public function scopeWithTranslations($query, $locale = null, $softDelete = null
142151
return $query;
143152
}
144153

145-
/**
146-
* Create new record.
147-
*
148-
* @param array $attributes
149-
*/
150-
public static function create(array $attributes)
151-
{
152-
$model = new static($attributes);
153-
154-
$model->save($attributes);
155-
156-
return $model;
157-
}
158-
159154
/**
160155
* Save record.
161156
*

0 commit comments

Comments
 (0)