Skip to content

Commit 6ec87a8

Browse files
committed
Update with request data when using validation
1 parent d5641d3 commit 6ec87a8

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ private function buildMethods(Controller $controller)
104104
}
105105

106106
$body = '';
107+
$using_validation = false;
108+
107109
foreach ($statements as $statement) {
108110
if ($statement instanceof SendStatement) {
109111
$body .= self::INDENT.$statement->output().PHP_EOL;
@@ -115,6 +117,7 @@ private function buildMethods(Controller $controller)
115117
$this->addImport($controller, config('blueprint.namespace').'\\Mail\\'.$statement->mail());
116118
}
117119
} elseif ($statement instanceof ValidateStatement) {
120+
$using_validation = true;
118121
$class_name = $controller->name().Str::studly($name).'Request';
119122

120123
$fqcn = config('blueprint.namespace').'\\Http\\Requests\\'.($controller->namespace() ? $controller->namespace().'\\' : '').$class_name;
@@ -153,7 +156,7 @@ private function buildMethods(Controller $controller)
153156
} elseif ($statement instanceof SessionStatement) {
154157
$body .= self::INDENT.$statement->output().PHP_EOL;
155158
} elseif ($statement instanceof EloquentStatement) {
156-
$body .= self::INDENT.$statement->output($controller->prefix(), $name).PHP_EOL;
159+
$body .= self::INDENT.$statement->output($controller->prefix(), $name, $using_validation).PHP_EOL;
157160
$this->addImport($controller, $this->determineModel($controller, $statement->reference()));
158161
} elseif ($statement instanceof QueryStatement) {
159162
$body .= self::INDENT.$statement->output($controller->prefix()).PHP_EOL;

src/Models/Statements/EloquentStatement.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function columns(): array
4444
return $this->columns;
4545
}
4646

47-
public function output(string $controller_prefix, string $context): string
47+
public function output(string $controller_prefix, string $context, bool $using_validation = false): string
4848
{
4949
$model = $this->determineModel($controller_prefix);
5050
$code = '';
@@ -61,14 +61,18 @@ public function output(string $controller_prefix, string $context): string
6161
}
6262

6363
if ($this->operation() == 'update') {
64-
$columns = '';
65-
if (!empty($this->columns())) {
66-
$columns = implode(', ', array_map(function ($column) {
67-
return sprintf("'%s' => \$%s", $column, $column);
68-
}, $this->columns()));
64+
if ($using_validation) {
65+
$code = "$" . Str::camel($model) . '->update($request->validated());';
66+
} else {
67+
$columns = '';
68+
if (!empty($this->columns())) {
69+
$columns = implode(', ', array_map(function ($column) {
70+
return sprintf("'%s' => \$%s", $column, $column);
71+
}, $this->columns()));
72+
}
73+
74+
$code = "$" . Str::camel($model) . '->update([' . $columns . ']);';
6975
}
70-
71-
$code = "$" . Str::camel($model) . '->update([' . $columns . ']);';
7276
}
7377

7478
if ($this->operation() == 'find') {

tests/fixtures/controllers/certificate-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function show(Request $request, Certificate $certificate)
5050
*/
5151
public function update(CertificateUpdateRequest $request, Certificate $certificate)
5252
{
53-
$certificate->update([]);
53+
$certificate->update($request->validated());
5454

5555
return new CertificateResource($certificate);
5656
}

tests/fixtures/controllers/certificate-type-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function show(Request $request, CertificateType $certificateType)
5050
*/
5151
public function update(CertificateTypeUpdateRequest $request, CertificateType $certificateType)
5252
{
53-
$certificateType->update([]);
53+
$certificateType->update($request->validated());
5454

5555
return new CertificateTypeResource($certificateType);
5656
}

tests/fixtures/controllers/custom-models-namespace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function show(Request $request, Tag $tag)
5050
*/
5151
public function update(TagUpdateRequest $request, Tag $tag)
5252
{
53-
$tag->update([]);
53+
$tag->update($request->validated());
5454

5555
return new TagResource($tag);
5656
}

0 commit comments

Comments
 (0)