|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Lomkit\Rest\Concerns\Relations; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use Lomkit\Rest\Contracts\QueryBuilder; |
| 7 | +use Lomkit\Rest\Relations\Relation; |
| 8 | + |
| 9 | +trait PerformsRelationOperations |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @param Model $model |
| 13 | + * @param Relation $relation |
| 14 | + * @param array $mutation |
| 15 | + * @param array $attributes |
| 16 | + * |
| 17 | + * @return void |
| 18 | + */ |
| 19 | + public function create(Model $model, Relation $relation, array $mutation = [], array $attributes = []) |
| 20 | + { |
| 21 | + $toPerformActionModel = app()->make(QueryBuilder::class, ['resource' => $relation->resource()]) |
| 22 | + ->applyMutation($mutation, $attributes); |
| 23 | + |
| 24 | + $this->resource()->authorizeToAttach($model, $toPerformActionModel); |
| 25 | + |
| 26 | + $model |
| 27 | + ->{$relation->relation}() |
| 28 | + ->syncWithoutDetaching( |
| 29 | + [ |
| 30 | + $toPerformActionModel->getKey() => $mutation['pivot'] ?? [], |
| 31 | + ] |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param Model $model |
| 37 | + * @param Relation $relation |
| 38 | + * @param array $mutation |
| 39 | + * @param array $attributes |
| 40 | + * |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function update(Model $model, Relation $relation, array $mutation = [], array $attributes = []) |
| 44 | + { |
| 45 | + $toPerformActionModels = app()->make(QueryBuilder::class, ['resource' => $relation->resource()]) |
| 46 | + ->mutations($mutation, $attributes); |
| 47 | + |
| 48 | + foreach ($toPerformActionModels as $toPerformActionModel) { |
| 49 | + $this->resource()->authorizeToAttach($model, $toPerformActionModel); |
| 50 | + } |
| 51 | + |
| 52 | + $model->{$relation->relation}() |
| 53 | + ->syncWithoutDetaching( |
| 54 | + collect($toPerformActionModels) |
| 55 | + ->mapWithKeys( |
| 56 | + fn (Model $toPerformActionModel) => [$toPerformActionModel->getKey() => $mutation['pivot'] ?? []] |
| 57 | + )->toArray() |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param Model $model |
| 63 | + * @param Relation $relation |
| 64 | + * @param array $mutation |
| 65 | + * @param array $attributes |
| 66 | + * |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + public function attach(Model $model, Relation $relation, array $mutation = [], array $attributes = []) |
| 70 | + { |
| 71 | + $toPerformActionModels = app()->make(QueryBuilder::class, ['resource' => $relation->resource()]) |
| 72 | + ->mutations($mutation, $attributes); |
| 73 | + |
| 74 | + foreach ($toPerformActionModels as $toPerformActionModel) { |
| 75 | + $this->resource()->authorizeToAttach($model, $toPerformActionModel); |
| 76 | + } |
| 77 | + |
| 78 | + $model->{$relation->relation}() |
| 79 | + ->attach( |
| 80 | + collect($toPerformActionModels) |
| 81 | + ->mapWithKeys( |
| 82 | + fn (Model $toPerformActionModel) => [$toPerformActionModel->getKey() => $mutation['pivot'] ?? []] |
| 83 | + )->toArray() |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param Model $model |
| 89 | + * @param Relation $relation |
| 90 | + * @param array $mutation |
| 91 | + * @param array $attributes |
| 92 | + * |
| 93 | + * @return void |
| 94 | + */ |
| 95 | + public function detach(Model $model, Relation $relation, array $mutation = [], array $attributes = []) |
| 96 | + { |
| 97 | + $toPerformActionModels = app()->make(QueryBuilder::class, ['resource' => $relation->resource()]) |
| 98 | + ->mutations($mutation, $attributes); |
| 99 | + |
| 100 | + foreach ($toPerformActionModels as $toPerformActionModel) { |
| 101 | + $this->resource()->authorizeToDetach($model, $toPerformActionModel); |
| 102 | + } |
| 103 | + |
| 104 | + $model->{$relation->relation}()->detach($toPerformActionModels); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param Model $model |
| 109 | + * @param Relation $relation |
| 110 | + * @param array $mutation |
| 111 | + * @param array $attributes |
| 112 | + * |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + public function toggle(Model $model, Relation $relation, array $mutation = [], array $attributes = []) |
| 116 | + { |
| 117 | + $toPerformActionModels = app()->make(QueryBuilder::class, ['resource' => $relation->resource()]) |
| 118 | + ->mutations($mutation, $attributes); |
| 119 | + |
| 120 | + $results = $model->{$relation->relation}() |
| 121 | + ->toggle( |
| 122 | + collect($toPerformActionModels) |
| 123 | + ->mapWithKeys( |
| 124 | + fn (Model $toPerformActionModel) => [$toPerformActionModel->getKey() => $mutation['pivot'] ?? []] |
| 125 | + )->toArray(), |
| 126 | + ); |
| 127 | + |
| 128 | + foreach ($results['attached'] as $attached) { |
| 129 | + $this->resource()->authorizeToAttach($model, $relation->resource()::$model::find($attached)); |
| 130 | + } |
| 131 | + |
| 132 | + foreach ($results['detached'] as $detached) { |
| 133 | + $this->resource()->authorizeToDetach($model, $relation->resource()::$model::find($detached)); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @param Model $model |
| 139 | + * @param Relation $relation |
| 140 | + * @param array $mutation |
| 141 | + * @param array $attributes |
| 142 | + * @param bool $withoutDetaching |
| 143 | + * |
| 144 | + * @return void |
| 145 | + */ |
| 146 | + public function sync(Model $model, Relation $relation, array $mutation = [], array $attributes = [], $withoutDetaching = false) |
| 147 | + { |
| 148 | + $toPerformActionModels = app()->make(QueryBuilder::class, ['resource' => $relation->resource()]) |
| 149 | + ->mutations($mutation, $attributes); |
| 150 | + |
| 151 | + $results = $model->{$relation->relation}() |
| 152 | + ->sync( |
| 153 | + collect($toPerformActionModels) |
| 154 | + ->mapWithKeys( |
| 155 | + fn (Model $toPerformActionModel) => [$toPerformActionModel->getKey() => $mutation['pivot'] ?? []] |
| 156 | + )->toArray(), |
| 157 | + $withoutDetaching |
| 158 | + ); |
| 159 | + |
| 160 | + foreach ($results['attached'] as $attached) { |
| 161 | + $this->resource()->authorizeToAttach($model, $relation->resource()::$model::find($attached)); |
| 162 | + } |
| 163 | + |
| 164 | + foreach ($results['detached'] as $detached) { |
| 165 | + $this->resource()->authorizeToDetach($model, $relation->resource()::$model::find($detached)); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments