Alexey
Alexey

Reputation: 91

Laravel how to modify custom request from middleware

I have middleware that modifies requests by adding some data retrieved from DB:

public function handle($request, Closure $next, ...$guards)
{
     $request->merge(['newData' => [...]]);

     return $next($request);
}

And everything worked just fine until I decided to use my custom request with some validation instead of the default one.

 public function getSomeDataWithCustomRequest(CustomRequest $request): string
{
    // $request doesn't have newData attribute, while request() has
    dd($request->newData, request()->newData);
}

The problem is that the custom request doesn't have new data from middleware, while the request retrieved via request() in the same function has it. I also had an idea to just create a custom request instance in middleware, copy all data from the base request there and return it, but I wasn't able to fully copy all data from base request to custom.

Is there any way to modify custom requests from middleware?

Upvotes: 2

Views: 1192

Answers (0)

Related Questions