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

Viewed 368

I have created the form request for data validation, I have also created the middleware for request modify which add the one key in the \Illuminate\Http\Request object from taking the value from query parameter

I want \Illuminate\Http\Request object data which is modified by the middleware to be attached with form request so I can further user in controller with the $request object

I have written middleware code to the baseformrequest(custom class) and called the method into my formrequest.

middleware code:

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);
    }

I expect all the modified request object can be accessible in My form request object, but I can't access in my Laravel controller

1 Answers

You can add custom attributes to the request object like this

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

Then you can access it using

$request->get('with');
Related