From cf13781aa11ed56c2eea49622b20e9a38cbc1183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Thu, 4 May 2023 23:33:13 +0200 Subject: [PATCH] rework persiting logic --- src/Fields/BelongsToMany.php | 6 ++---- src/Fields/Computed.php | 2 +- src/Fields/Field.php | 8 +++----- src/Fields/Fieldset.php | 8 +++----- src/Fields/HasOneOrMany.php | 6 ++---- src/Fields/Media.php | 6 ++---- src/Http/Controllers/RelationController.php | 4 ++-- src/Http/Controllers/ResourceController.php | 4 ++-- src/Support/Collections/Fields.php | 12 ++++++++++++ tests/Fields/FieldTest.php | 6 +++++- 10 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 1e4ccd1d..1d92f624 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -21,11 +21,9 @@ public function getRelation(Model $model): EloquentRelation /** * {@inheritdoc} */ - public function persist(Request $request, Model $model): void + public function persist(Request $request, Model $model, mixed $value): void { - $model->saved(function (Model $model) use ($request): void { - $value = $this->getValueForHydrate($request, $model); - + $model->saved(function (Model $model) use ($request, $value): void { $this->resolveHydrate($request, $model, $value); $this->getRelation($model)->sync($value); diff --git a/src/Fields/Computed.php b/src/Fields/Computed.php index 1c956ed2..fbfe9875 100644 --- a/src/Fields/Computed.php +++ b/src/Fields/Computed.php @@ -38,7 +38,7 @@ public function searchable(bool|Closure $value = true): static /** * {@inheritdoc} */ - public function persist(Request $request, Model $model): void + public function persist(Request $request, Model $model, mixed $value): void { // } diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 276cd880..691af1b5 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -275,12 +275,10 @@ public function resolveFormat(Request $request, Model $model): mixed /** * Persist the request value on the model. */ - public function persist(Request $request, Model $model): void + public function persist(Request $request, Model $model, mixed $value): void { - $model->saving(function (Model $model) use ($request): void { - $this->resolveHydrate( - $request, $model, $this->getValueForHydrate($request, $model) - ); + $model->saving(function (Model $model) use ($request, $value): void { + $this->resolveHydrate($request, $model, $value); }); } diff --git a/src/Fields/Fieldset.php b/src/Fields/Fieldset.php index e371c645..11d037cd 100644 --- a/src/Fields/Fieldset.php +++ b/src/Fields/Fieldset.php @@ -48,14 +48,12 @@ public function registerRoutes(Router $router): void /** * {@inheritdoc} */ - public function persist(Request $request, Model $model): void + public function persist(Request $request, Model $model, mixed $value): void { - $this->resolveHydrate($request, $model, $this->getValueForHydrate($request, $model)); - $this->resolveFields($request) ->authorized($request, $model) - ->each(static function (Field $field) use ($request, $model): void { - $field->persist($request, $model); + ->each(static function (Field $field) use ($request, $model, $value): void { + $field->persist($request, $model, $value[$field->getKey()] ?? null); }); } diff --git a/src/Fields/HasOneOrMany.php b/src/Fields/HasOneOrMany.php index 617c9540..01a6c894 100644 --- a/src/Fields/HasOneOrMany.php +++ b/src/Fields/HasOneOrMany.php @@ -20,13 +20,11 @@ public function getRelation(Model $model): EloquentRelation /** * {@inheritdoc} */ - public function persist(Request $request, Model $model): void + public function persist(Request $request, Model $model, mixed $value): void { - $model->saved(function (Model $model) use ($request): void { + $model->saved(function (Model $model) use ($request, $value): void { $relation = $this->getRelation($model); - $value = $this->getValueForHydrate($request, $model); - $this->resolveHydrate($request, $model, $value); $models = $model->getRelation($this->getRelationName()); diff --git a/src/Fields/Media.php b/src/Fields/Media.php index 564c3734..3bec3b70 100644 --- a/src/Fields/Media.php +++ b/src/Fields/Media.php @@ -101,11 +101,9 @@ public function store(Request $request, string $path): Medium /** * {@inheritdoc} */ - public function persist(Request $request, Model $model): void + public function persist(Request $request, Model $model, mixed $value): void { - $model->saved(function (Model $model) use ($request): void { - $value = $this->getValueForHydrate($request, $model); - + $model->saved(function (Model $model) use ($request, $value): void { $this->resolveHydrate($request, $model, $value); $this->getRelation($model)->sync($value); diff --git a/src/Http/Controllers/RelationController.php b/src/Http/Controllers/RelationController.php index 24c1bb2b..ea47aa11 100644 --- a/src/Http/Controllers/RelationController.php +++ b/src/Http/Controllers/RelationController.php @@ -61,7 +61,7 @@ public function store(Request $request, Model $model): RedirectResponse $request->validate($fields->mapToValidate($request, $item->model)); - $fields->each->persist($request, $item->model); + $fields->persist($request, $item->model); $item->model->save(); @@ -116,7 +116,7 @@ public function update(Request $request, Model $model, Model $related): Redirect $request->validate($fields->mapToValidate($request, $item->model)); - $fields->each->persist($request, $item->model); + $fields->persist($request, $item->model); $item->model->save(); diff --git a/src/Http/Controllers/ResourceController.php b/src/Http/Controllers/ResourceController.php index c75e0e8c..31802362 100644 --- a/src/Http/Controllers/ResourceController.php +++ b/src/Http/Controllers/ResourceController.php @@ -77,7 +77,7 @@ public function store(Request $request): RedirectResponse $request->validate($fields->mapToValidate($request, $model)); - $fields->each->persist($request, $model); + $fields->persist($request, $model); $model->save(); @@ -138,7 +138,7 @@ public function update(Request $request, Model $model): RedirectResponse $request->validate($fields->mapToValidate($request, $model)); - $fields->each->persist($request, $model); + $fields->persist($request, $model); $model->save(); diff --git a/src/Support/Collections/Fields.php b/src/Support/Collections/Fields.php index c01a1dd6..0152701a 100644 --- a/src/Support/Collections/Fields.php +++ b/src/Support/Collections/Fields.php @@ -56,6 +56,18 @@ public function sortable(Request $request): static return $this->filter->isSortable($request); } + /** + * Persist the request value on the model. + */ + public function persist(Request $request, Model $model): void + { + $this->each(static function (Field $field) use ($request, $model): void { + $field->persist( + $request, $model, $field->getValueForHydrate($request, $model) + ); + }); + } + /** * Map the fields to display. */ diff --git a/tests/Fields/FieldTest.php b/tests/Fields/FieldTest.php index 514d86ec..02e56df6 100644 --- a/tests/Fields/FieldTest.php +++ b/tests/Fields/FieldTest.php @@ -218,7 +218,11 @@ public function a_field_persists_model_attribute() $this->assertNull($model->title); - $this->field->persist($this->app['request'], $model); + $this->field->persist( + $this->app['request'], + $model, + $this->field->getValueForHydrate($this->app['request'], $model) + ); $model->save();