Skip to content

Commit

Permalink
Update with request data when using validation (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored Jul 16, 2020
1 parent d5641d3 commit 66de68d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ private function buildMethods(Controller $controller)
}

$body = '';
$using_validation = false;

foreach ($statements as $statement) {
if ($statement instanceof SendStatement) {
$body .= self::INDENT.$statement->output().PHP_EOL;
Expand All @@ -115,6 +117,7 @@ private function buildMethods(Controller $controller)
$this->addImport($controller, config('blueprint.namespace').'\\Mail\\'.$statement->mail());
}
} elseif ($statement instanceof ValidateStatement) {
$using_validation = true;
$class_name = $controller->name().Str::studly($name).'Request';

$fqcn = config('blueprint.namespace').'\\Http\\Requests\\'.($controller->namespace() ? $controller->namespace().'\\' : '').$class_name;
Expand Down Expand Up @@ -153,7 +156,7 @@ private function buildMethods(Controller $controller)
} elseif ($statement instanceof SessionStatement) {
$body .= self::INDENT.$statement->output().PHP_EOL;
} elseif ($statement instanceof EloquentStatement) {
$body .= self::INDENT.$statement->output($controller->prefix(), $name).PHP_EOL;
$body .= self::INDENT.$statement->output($controller->prefix(), $name, $using_validation).PHP_EOL;
$this->addImport($controller, $this->determineModel($controller, $statement->reference()));
} elseif ($statement instanceof QueryStatement) {
$body .= self::INDENT.$statement->output($controller->prefix()).PHP_EOL;
Expand Down
11 changes: 7 additions & 4 deletions src/Models/Statements/EloquentStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function columns(): array
return $this->columns;
}

public function output(string $controller_prefix, string $context): string
public function output(string $controller_prefix, string $context, bool $using_validation = false): string
{
$model = $this->determineModel($controller_prefix);
$code = '';
Expand All @@ -61,14 +61,17 @@ public function output(string $controller_prefix, string $context): string
}

if ($this->operation() == 'update') {
$columns = '';
if (!empty($this->columns())) {
$columns = implode(', ', array_map(function ($column) {
return sprintf("'%s' => \$%s", $column, $column);
}, $this->columns()));
}

$code = "$" . Str::camel($model) . '->update([' . $columns . ']);';
$code = "$" . Str::camel($model) . '->update([' . $columns . ']);';
} elseif ($using_validation) {
$code = "$" . Str::camel($model) . '->update($request->validated());';
} else {
$code = "$" . Str::camel($model) . '->update([]);';
}
}

if ($this->operation() == 'find') {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/controllers/certificate-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function show(Request $request, Certificate $certificate)
*/
public function update(CertificateUpdateRequest $request, Certificate $certificate)
{
$certificate->update([]);
$certificate->update($request->validated());

return new CertificateResource($certificate);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/controllers/certificate-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function show(Request $request, CertificateType $certificateType)
*/
public function update(CertificateTypeUpdateRequest $request, CertificateType $certificateType)
{
$certificateType->update([]);
$certificateType->update($request->validated());

return new CertificateTypeResource($certificateType);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/controllers/custom-models-namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function show(Request $request, Tag $tag)
*/
public function update(TagUpdateRequest $request, Tag $tag)
{
$tag->update([]);
$tag->update($request->validated());

return new TagResource($tag);
}
Expand Down

0 comments on commit 66de68d

Please sign in to comment.