Skip to content

Commit 94bae45

Browse files
authored
Merge pull request #75 from Lomkit/feature/sync-toggle-relations-on-mutation
✨ mutate sync /toggle
2 parents b31aa6f + 1706379 commit 94bae45

File tree

4 files changed

+481
-4
lines changed

4 files changed

+481
-4
lines changed

src/Query/Traits/PerformMutation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function applyMutation(array $mutation = [], $attributes = [])
6464
);
6565
}
6666

67-
if ($mutation['operation'] === 'update') {
67+
if (in_array($mutation['operation'], ['update', 'touch', 'sync'])) {
6868
$model = $this->resource::newModel()::findOrFail($mutation['key']);
6969

7070
$this->resource->authorizeTo('update', $model);

src/Relations/BelongsToMany.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function afterMutating(Model $model, Relation $relation, array $mutationR
5454
->applyMutation($mutationRelation)
5555
->getKey()
5656
);
57-
} else {
57+
} elseif ($mutationRelation['operation'] === 'attach') {
5858
$model
5959
->{$relation->relation}()
6060
->attach(
@@ -64,6 +64,37 @@ public function afterMutating(Model $model, Relation $relation, array $mutationR
6464
->getKey() => $mutationRelation['pivot'] ?? [],
6565
]
6666
);
67+
} elseif ($mutationRelation['operation'] === 'toggle') {
68+
$model
69+
->{$relation->relation}()
70+
->toggle(
71+
[
72+
app()->make(QueryBuilder::class, ['resource' => $relation->resource()])
73+
->applyMutation($mutationRelation)
74+
->getKey() => $mutationRelation['pivot'] ?? [],
75+
]
76+
);
77+
} elseif ($mutationRelation['operation'] === 'sync') {
78+
$model
79+
->{$relation->relation}()
80+
->sync(
81+
[
82+
app()->make(QueryBuilder::class, ['resource' => $relation->resource()])
83+
->applyMutation($mutationRelation)
84+
->getKey() => $mutationRelation['pivot'] ?? [],
85+
],
86+
!isset($mutationRelation['without_detaching']) || !$mutationRelation['without_detaching']
87+
);
88+
} elseif (in_array($mutationRelation['operation'], ['create', 'update'])) {
89+
$model
90+
->{$relation->relation}()
91+
->syncWithoutDetaching(
92+
[
93+
app()->make(QueryBuilder::class, ['resource' => $relation->resource()])
94+
->applyMutation($mutationRelation)
95+
->getKey() => $mutationRelation['pivot'] ?? [],
96+
]
97+
);
6798
}
6899
}
69100
}

src/Rules/MutateRules.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
8686
[
8787
$attribute.'.operation' => [
8888
'required_with:'.$attribute,
89-
Rule::in('create', 'update', ...($this->isRootValidation ? [] : ['attach', 'detach'])),
89+
Rule::in('create', 'update', ...($this->isRootValidation ? [] : ['attach', 'detach', 'toggle', 'sync'])),
9090
],
9191
$attribute.'.attributes' => [
9292
'prohibited_if:'.$attribute.'.operation,attach',
@@ -97,10 +97,15 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
9797
'required_if:'.$attribute.'.operation,update',
9898
'required_if:'.$attribute.'.operation,attach',
9999
'required_if:'.$attribute.'.operation,detach',
100+
'required_if:'.$attribute.'.operation,toggle',
101+
'required_if:'.$attribute.'.operation,sync',
100102
'prohibited_if:'.$attribute.'.operation,create',
101103
'exists:'.$this->resource::newModel()->getTable().','.$this->resource::newModel()->getKeyName(),
102104
],
103-
105+
$attribute.'.without_detaching' => [
106+
'boolean',
107+
'prohibited_unless:'.$attribute.'.operation,sync',
108+
],
104109
$attribute.'.relations.*' => [
105110
new MutateItemRelations($this->resource, $this->request),
106111
],

0 commit comments

Comments
 (0)