@@ -112,6 +112,7 @@ protected function storeRelated($item, array $includes, array $data): void
112
112
throw new ApiException ("$ type mapping not implemented yet " );
113
113
break ;
114
114
}
115
+ $ item ->load ($ with );
115
116
}
116
117
}
117
118
@@ -184,23 +185,38 @@ protected function processBelongsToManyRelation($relation, array $relatedRecords
184
185
$ model = $ relation ->getRelated ();
185
186
$ sync = collect ();
186
187
188
+ $ pivots = $ relation ->getPivotColumns ();
189
+ $ detach = filter_var (request ()->get ('sync ' )[$ with ] ?? false , FILTER_VALIDATE_BOOLEAN );
190
+
187
191
foreach ($ relatedRecords as $ relatedRecord ) {
192
+
188
193
if (! isset ($ relatedRecord [$ parentKey ])) {
189
194
$ relatedRecord [$ parentKey ] = $ item ->getAttribute ($ relatedKey );
190
195
}
191
196
if ($ relatedRecord [$ parentKey ]) {
192
197
$ existanceCheck = [$ parentKey => $ relatedRecord [$ parentKey ]];
193
- $ sync -> push ( $ model ->updateOrCreate ($ existanceCheck , $ relatedRecord) );
198
+ $ record = $ model ->updateOrCreate ($ existanceCheck , $ relatedRecord );
194
199
} elseif (isset ($ data [$ relatedKey ])) {
195
200
$ existanceCheck = [$ parentKey => $ data [$ relatedKey ]];
196
- $ sync -> push ( $ model ->updateOrCreate ($ existanceCheck , $ relatedRecord) );
201
+ $ record = $ model ->updateOrCreate ($ existanceCheck , $ relatedRecord );
197
202
} else {
198
- $ sync -> push ( $ model ->create ($ relatedRecord) );
203
+ $ record = $ model ->create ($ relatedRecord );
199
204
}
200
- }
201
205
202
- $ detach = filter_var (request ()->get ('sync ' )[$ with ] ?? false , FILTER_VALIDATE_BOOLEAN );
206
+ $ pvals = [];
207
+ if ($ pivots ){
208
+
209
+ foreach ($ pivots as $ pivot )
210
+ {
211
+ if (isset ($ relatedRecord [$ pivot ])){
212
+ $ pvals [$ pivot ] = $ relatedRecord [$ pivot ];
213
+ }
214
+ }
215
+ }
216
+ $ sync ->put ($ record ->getKey (), $ pvals );
217
+
218
+ }
203
219
204
- $ relation ->sync ($ sync ->pluck ( ' id ' ), $ detach );
220
+ $ relation ->sync ($ sync ->toArray ( ), $ detach );
205
221
}
206
222
}
0 commit comments