Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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