9
9
use A17 \Twill \Repositories \ModuleRepository ;
10
10
use Carbon \Carbon ;
11
11
use Exception ;
12
+ use Illuminate \Database \Eloquent \Relations \BelongsTo ;
13
+ use Illuminate \Database \Eloquent \Relations \HasOneOrMany ;
14
+ use Illuminate \Database \Eloquent \Relations \Relation ;
12
15
use Illuminate \Support \Arr ;
13
16
use Illuminate \Support \Collection ;
14
17
use Illuminate \Support \Str ;
@@ -124,6 +127,7 @@ public function updateRepeaterMorphMany(
124
127
125
128
// @todo: This needs refactoring in 3.x
126
129
foreach ($ relationFields as $ index => $ relationField ) {
130
+ $ this ->extractRepeaters ($ repeaterName , $ fields , $ relationField );
127
131
$ relationField ['position ' ] = $ index + 1 ;
128
132
$ relationField [$ morphFieldId ] = $ object ->id ;
129
133
$ relationField [$ morphFieldType ] = $ object ->getMorphClass ();
@@ -186,6 +190,7 @@ public function updateRepeaterWithPivot(
186
190
$ currentRelations = $ object ->{$ relation }()->withPivot ('id ' )->get ();
187
191
188
192
foreach ($ relationFields as $ index => $ relationField ) {
193
+ $ this ->extractRepeaters ($ repeaterName , $ fields , $ relationField );
189
194
$ relationField ['position ' ] = $ index + 1 ;
190
195
191
196
// If the relation is not an "existing" one try to match it with our session.
@@ -280,20 +285,30 @@ public function updateRepeater(
280
285
281
286
$ relationRepository = $ this ->getModelRepository ($ relation , $ modelOrRepository );
282
287
288
+ if (method_exists ($ this ->model , $ relation )) {
289
+ /** @var Relation $relationInstance */
290
+ $ relationInstance = $ this ->model ->$ relation ();
291
+ if ($ relationInstance instanceof BelongsTo || $ relationInstance instanceof HasOneOrMany) {
292
+ $ fk = $ relationInstance ->getForeignKeyName ();
293
+ }
294
+ }
295
+ $ fk ??= $ this ->model ->getForeignKey ();
296
+
283
297
// If no relation field submitted, soft deletes all associated rows.
284
298
// We only do this when the model is already existing.
285
299
if (! $ relationFields && ! $ object ->wasRecentlyCreated ) {
286
300
$ relationRepository ->updateBasic (null , [
287
301
'deleted_at ' => Carbon::now (),
288
302
], [
289
- $ this -> model -> getForeignKey () => $ object ->id ,
303
+ $ fk => $ object ->id ,
290
304
]);
291
305
}
292
306
293
307
// keep a list of updated and new rows to delete (soft delete?) old rows that were deleted from the frontend
294
308
$ currentIdList = [];
295
309
296
310
foreach ($ relationFields as $ index => $ relationField ) {
311
+ $ this ->extractRepeaters ($ repeaterName , $ fields , $ relationField );
297
312
$ relationField ['position ' ] = $ index + 1 ;
298
313
// If the relation is not an "existing" one try to match it with our session.
299
314
if (
@@ -322,7 +337,7 @@ public function updateRepeater(
322
337
$ currentIdList [] = (int )$ id ;
323
338
} else {
324
339
// new row, let's attach to our object and create
325
- $ relationField [$ this -> model -> getForeignKey () ] = $ object ->id ;
340
+ $ relationField [$ fk ] = $ object ->id ;
326
341
$ frontEndId = $ relationField ['id ' ];
327
342
unset($ relationField ['id ' ]);
328
343
$ newRelation = $ relationRepository ->create ($ relationField );
@@ -344,6 +359,21 @@ public function updateRepeater(
344
359
}
345
360
}
346
361
362
+ private function extractRepeaters (string $ repeaterName , array $ fields , ?array &$ relationField ): void
363
+ {
364
+ if (!isset ($ relationField ) || empty ($ fields ['repeaters ' ])) {
365
+ return ;
366
+ }
367
+
368
+ $ prefix = $ repeaterName . '|blocks- ' . $ relationField ['id ' ] . '| ' ;
369
+ foreach ($ fields ['repeaters ' ] as $ key => $ repeater ) {
370
+ if (str_starts_with ($ key , $ prefix )) {
371
+ unset($ fields ['repeaters ' ][$ key ]);
372
+ $ relationField ['repeaters ' ][substr ($ key , strlen ($ prefix ))] = $ repeater ;
373
+ }
374
+ }
375
+ }
376
+
347
377
/**
348
378
* This makes sure that arrays are json encode (translations).
349
379
*/
0 commit comments