Skip to content

Commit 336d79b

Browse files
♻️ Change beforeMutating for MorphedByMany relation (#167)
* ♻️ Change beforeMutating for MorphedByMany relation * Add tests * Add StyleCI * Fix feedback --------- Co-authored-by: Thomas <[email protected]>
1 parent 0225504 commit 336d79b

File tree

2 files changed

+416
-4
lines changed

2 files changed

+416
-4
lines changed

src/Relations/MorphedByMany.php

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,78 @@ public function afterMutating(Model $model, Relation $relation, array $mutationR
4747
{
4848
foreach ($mutationRelations[$relation->relation] as $mutationRelation) {
4949
if ($mutationRelation['operation'] === 'detach') {
50+
$toDetachModel = app()->make(QueryBuilder::class, ['resource' => $relation->resource()])
51+
->applyMutation($mutationRelation);
52+
53+
$this->resource()->authorizeToDetach($model, $toDetachModel);
54+
5055
$model
5156
->{$relation->relation}()
5257
->detach(
53-
app()->make(QueryBuilder::class, ['resource' => $relation->resource()])
54-
->applyMutation($mutationRelation)
55-
->getKey()
58+
$toDetachModel->getKey()
5659
);
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+
5866
$model
5967
->{$relation->relation}()
6068
->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(
6195
[
6296
app()->make(QueryBuilder::class, ['resource' => $relation->resource()])
6397
->applyMutation($mutationRelation)
6498
->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'] ?? [],
65122
]
66123
);
67124
}

0 commit comments

Comments
 (0)