File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -823,7 +823,7 @@ protected function syncHasOneOrMany(HasOneOrMany $rel, $ids): array
823823 if ($ ids === null ) {
824824 $ ids = [];
825825 } elseif (!\is_array ($ ids )) {
826- return $ res ;
826+ $ ids = [ $ ids ] ;
827827 }
828828
829829 $ fk = $ rel ->getForeignKeyName ();
@@ -832,11 +832,16 @@ protected function syncHasOneOrMany(HasOneOrMany $rel, $ids): array
832832 /** @var Collection $toDelete */
833833 $ toDelete = $ rel ->get ()->keyBy ($ rel ->getRelated ()->getKeyName ());
834834
835+ /** @var Model[] $toSyncItems */
836+ $ toSyncItems = [];
837+
835838 /** @var Model $item */
836839 foreach ($ toSync as $ item ) {
837840 if ($ item ->getAttribute ($ fk ) !== $ parentKey ) {
838841 $ item ->setAttribute ($ fk , $ parentKey );
839- $ item ->save ();
842+ // items syncing will be actually sync after deleting relations
843+ // to prevent database unique checks failure
844+ $ toSyncItems [] = $ item ;
840845 $ res ['attached ' ][] = $ item ->getKey ();
841846 }
842847 if ($ toDelete ->has ($ item ->getKey ())) {
@@ -851,6 +856,10 @@ protected function syncHasOneOrMany(HasOneOrMany $rel, $ids): array
851856 $ res ['detached ' ][] = $ item ->getKey ();
852857 }
853858
859+ foreach ($ toSyncItems as $ item ) {
860+ $ item ->save ();
861+ }
862+
854863 return $ res ;
855864 }
856865
You can’t perform that action at this time.
0 commit comments