Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to manage cookies in your Laravel application

Zubair Idris Aweda

What is a cookie?

Cookies are small blocks of data created by a web server while browsing a website. They are placed on the user’s computer by the user’s web browser.

Cookies are placed so a user can access a website, and more than one cookie may be placed on a user’s device during a session. Cookies are especially useful for processes like authentication.

Setting a cookie

To set a cookie, use the cookie method on the response object. The method accepts a required name parameter.

The second parameter is the value of the cookie.

An expiry date can be set as well. After the specified time, the cookie will be removed from the user’s browser.

Other parameters have default values.


public function cookie(string $name, string $value = null, $expire = 0) {};

To see other parameters and the actual function definition, look here.


For example, you can set a cookie to store my name like so:

Route::get('/', function () {
    return response('Hello')->cookie('name', 'Zubair Idris Aweda');

This cookie is signed and encrypted by Laravel. You may see the encrypted cookie by checking your browser’s dev toolsdevelopment tools.

Development tools options in Mac OS

Encrypted Cookie
Encrypted Cookie


This means that if these cookies are tampered with by the client, they are invalidated.

Retrieving a cookie

To access a cookie, use the cookie method on the request object like this:

Route::get('/', function () {
    $cookie1 = request()->cookie('name');

How to access and check dev tools for cookies

Since cookies are stored in the browser, they can be accessed, edited, or even removed completely using the browser developer tools.

Most recent browsers ship with the dev tools.

To access the dev tools, read your browser’s documentation on your operating system. They may vary from browser to browser.

They usually have variations in the commands depending on the user’s operating system.



View all Courses

Keep Exploring