Skip to content

Commit

Permalink
rework persiting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed May 4, 2023
1 parent 51caab3 commit cf13781
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 28 deletions.
6 changes: 2 additions & 4 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Computed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
//
}
Expand Down
8 changes: 3 additions & 5 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
8 changes: 3 additions & 5 deletions src/Fields/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
6 changes: 2 additions & 4 deletions src/Fields/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 2 additions & 4 deletions src/Fields/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
12 changes: 12 additions & 0 deletions src/Support/Collections/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
6 changes: 5 additions & 1 deletion tests/Fields/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit cf13781

Please sign in to comment.