Laravel Cookie

In this topic, we will learn about Cookie in Laravel.

Retrieving Cookies From Requests

All cookies stored in encrypted format, it can't be changed or read by the client. To retrieve a cookie value from the request, use the cookie method on a Illuminate/Http/Request instance:

$name = $request->cookie('name');

Alternatively, we may use the Cookie facade to access cookie values

use Illuminate/Support/Facades/Cookie;
$name = Cookie::get('name');

Attaching Cookies To Responses

The cookie can be attached to the response using the withCookie() method.

return response('Hello World')->cookie(
    'name', 'value', $minutes
);

Cookie() method will take 3 arguments. First is the name of the cookie, second is the value of the cookie and the third argument is the duration of the cookie after which the cookie will get deleted automatically.