我需要动态创建一个自定义请求,并将请求传递给Form Request。我找到的最接近的答案是this SO answer。但是,当我运行$request->validated()
时,它给我错误消息Call to a member function validated() on null in /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php:221
。
下面是完整的代码:
功能A
private $service;
public function __construct(
Service $service,
) {
$this->service = $service;
}
...
$data = [
"variable_a" => "string",
'variable_b' => true,
];
$request = new Request();
$request->replace($data);
$customFormRequest = new CustomFormRequest();
$validate = $customFormRequest::createFrom(
$request
);
$return_data = $this->service->project($validate);
服务
public function project(CustomFormRequest $request)
{
\Log::debug($request);
$data = $request->validated();
\Log::debug($data);
}
注意在服务内部登录时,$request
和$request->all()
具有数组值
1条答案
按热度按时间vlju58qv1#
它不起作用的原因是因为您只获取了CustomFormRequest的一个示例。自定义窗体请求只有在容器外解析时才被验证。请改用验证程序:
我假设此时没有理由将CustomFormRequest的示例传递给
project()
,因为数据已经过验证。只需更新类型: