如何将所有 \Illuminate\Http\Request $request 数据设置为 FormRequest 对象以便在控制器中使用 LARAVEL

How to set all the \Illuminate\Http\Request $request data to FormRequest object for use in controller In LARAVEL

我已经创建了用于数据验证的表单请求, 我还为请求修改创建了中间件,它通过从查询参数

中获取值来在 \Illuminate\Http\Request 对象中添加一个键

我希望 \Illuminate\Http\Request 由中间件修改的对象数据附加到表单请求中,这样我就可以在控制器中进一步使用 $request 对象

我已将中间件代码写入 baseformrequest(custom class) 并将该方法调用到我的 formrequest

中间件代码:

public function handle($request, Closure $next)
    {
        $requested_method = $request->route()->getActionMethod();
        $required_with = [];
        if($requested_method == 'show'){
            if (!is_null($request->get('_with'))) {
                $required_with = explode(',', $request->get('_with'));
            }
        }

        $request->with = $request->get('_with');
        return $next($request);
    }

我希望所有修改后的请求对象都可以在我的表单请求对象中访问,但我无法在我的 Laravel 控制器中访问

您可以像这样向请求对象添加自定义属性

$request->attributes->add(['with' => $required_with])

然后您可以使用

访问它
$request->get('with');