In this tutorial you will learn about the Laravel Cookie and its application with practical example.
Creating Cookie
In Laravel, cookies are created using the withCookie() method of a response instance of Illuminate\Http\Response class. All the Cookies generated by the laravel framework are encrypted and signed using an authentication token so that they can’t be modified by the client.
Syntax:-
1 2 3 4 5 6 7 |
//Call the withCookie() method with the response instance $response = new Illuminate\Http\Response('Hello World'); $response->withCookie('name', 'value', $minutes); return $response; |
Retrieving Cookie
Once a cookie is created, it can be retrieved from the request using the cookie method on the Illuminate\Http\Request instance.
Syntax:-
1 |
$value = $request->cookie('cookie_name'); |