@@ -47,21 +47,78 @@ public function afterMutating(Model $model, Relation $relation, array $mutationR
47
47
{
48
48
foreach ($ mutationRelations [$ relation ->relation ] as $ mutationRelation ) {
49
49
if ($ mutationRelation ['operation ' ] === 'detach ' ) {
50
+ $ toDetachModel = app ()->make (QueryBuilder::class, ['resource ' => $ relation ->resource ()])
51
+ ->applyMutation ($ mutationRelation );
52
+
53
+ $ this ->resource ()->authorizeToDetach ($ model , $ toDetachModel );
54
+
50
55
$ model
51
56
->{$ relation ->relation }()
52
57
->detach (
53
- app ()->make (QueryBuilder::class, ['resource ' => $ relation ->resource ()])
54
- ->applyMutation ($ mutationRelation )
55
- ->getKey ()
58
+ $ toDetachModel ->getKey ()
56
59
);
57
- } else {
60
+ } elseif ($ mutationRelation ['operation ' ] === 'attach ' ) {
61
+ $ toAttachModel = app ()->make (QueryBuilder::class, ['resource ' => $ relation ->resource ()])
62
+ ->applyMutation ($ mutationRelation );
63
+
64
+ $ this ->resource ()->authorizeToAttach ($ model , $ toAttachModel );
65
+
58
66
$ model
59
67
->{$ relation ->relation }()
60
68
->attach (
69
+ [
70
+ $ toAttachModel ->getKey () => $ mutationRelation ['pivot ' ] ?? [],
71
+ ]
72
+ );
73
+ } elseif ($ mutationRelation ['operation ' ] === 'toggle ' ) {
74
+ $ results = $ model
75
+ ->{$ relation ->relation }()
76
+ ->toggle (
77
+ [
78
+ app ()->make (QueryBuilder::class, ['resource ' => $ relation ->resource ()])
79
+ ->applyMutation ($ mutationRelation )
80
+ ->getKey () => $ mutationRelation ['pivot ' ] ?? [],
81
+ ]
82
+ );
83
+
84
+ foreach ($ results ['attached ' ] as $ attached ) {
85
+ $ this ->resource ()->authorizeToAttach ($ model , $ relation ->resource ()::$ model ::find ($ attached ));
86
+ }
87
+
88
+ foreach ($ results ['detached ' ] as $ detached ) {
89
+ $ this ->resource ()->authorizeToDetach ($ model , $ relation ->resource ()::$ model ::find ($ detached ));
90
+ }
91
+ } elseif ($ mutationRelation ['operation ' ] === 'sync ' ) {
92
+ $ results = $ model
93
+ ->{$ relation ->relation }()
94
+ ->sync (
61
95
[
62
96
app ()->make (QueryBuilder::class, ['resource ' => $ relation ->resource ()])
63
97
->applyMutation ($ mutationRelation )
64
98
->getKey () => $ mutationRelation ['pivot ' ] ?? [],
99
+ ],
100
+ !isset ($ mutationRelation ['without_detaching ' ]) || !$ mutationRelation ['without_detaching ' ]
101
+ );
102
+
103
+ foreach ($ results ['attached ' ] as $ attached ) {
104
+ $ this ->resource ()->authorizeToAttach ($ model , $ relation ->resource ()::$ model ::find ($ attached ));
105
+ }
106
+
107
+ foreach ($ results ['detached ' ] as $ detached ) {
108
+ $ this ->resource ()->authorizeToDetach ($ model , $ relation ->resource ()::$ model ::find ($ detached ));
109
+ }
110
+ } elseif (in_array ($ mutationRelation ['operation ' ], ['create ' , 'update ' ])) {
111
+ $ toAttachModel = app ()->make (QueryBuilder::class, ['resource ' => $ relation ->resource ()])
112
+ ->applyMutation ($ mutationRelation );
113
+
114
+ $ this ->resource ()->authorizeToAttach ($ model , $ toAttachModel );
115
+
116
+ $ model
117
+ ->{$ relation ->relation }()
118
+ ->syncWithoutDetaching (
119
+ [
120
+ $ toAttachModel
121
+ ->getKey () => $ mutationRelation ['pivot ' ] ?? [],
65
122
]
66
123
);
67
124
}
0 commit comments