如何从lalavel中的控制器中间件传递cookie作为参数?

I am a beginner in laravel. I am trying to pass a parameter from middleware from controller like this:

if(isset($_COOKIE['name']))
    {   
        return $next($request->attributes->add(['name' => strval($_COOKIE['name'])]));
    }

However, I got this error message:

"Type error: Argument 1 passed to Symfony\Component\HttpFoundation\Response::prepare() must be an instance of Symfony\Component\HttpFoundation\Request, null given"

I tried to convert the cookie value as string before passing it, but it did not work. How can I pass the pass the cookie as parameter? I do not want to get the value of cookie using $_COOKIE['name']. My laravel version is 5.5. Thank you!

Try merging the cookie value in with the request input:

$merged = $request->merge(['name' => $request->cookie('name')]);

return $next($merged);

Keep in mind this will overwrite any input values named name so use a unique key.

However, this isn't really necessary, as the request has access to the cookie throughout it's lifecycle:

// MyController or MyModel or MyServiceProvider...
if (request()->hasCookie('name')) {
    $cookie = request()->cookie('name');
    // do stuff with cookie
}