@@ -115,13 +115,24 @@ public function scopeWithTranslations($query, $locale = null, $softDelete = null
115
115
$ i18nId = $ this ->getI18nId ($ locale );
116
116
$ translatorTable = new $ this ->translator ();
117
117
$ 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
+ });
125
136
}
126
137
127
138
if ($ softDelete ) {
@@ -155,10 +166,6 @@ public function save(array $options = [])
155
166
if (empty ($ options )) {
156
167
$ options = \Input::all ();
157
168
}
158
- // if method is put or patch remove translations
159
- if (\Request::method () === 'PUT ' || \Request::method () === 'PATCH ' ) {
160
- $ this ->deleteTranslations ($ this ->id );
161
- }
162
169
163
170
$ translatableId = $ this ->saveTranslatable ($ options );
164
171
$ this ->saveTranslations ($ translatableId , $ options );
@@ -212,22 +219,21 @@ public function saveTranslations($translatableId, $options)
212
219
}
213
220
}
214
221
}
215
- $ translationModel = new $ this ->translator ();
216
222
217
- foreach ($ translationsArray as $ translationValues ) {
218
- $ translationModel ->create ($ translationValues );
219
- }
220
- }
223
+ $ modelName = $ this ->translator ;
221
224
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 ();
230
229
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
+ }
232
238
}
233
239
}
0 commit comments