In Laravel, cookies are used to store data on the client-side browser. Laravel provides a convenient way to manage cookies using the Illuminate\Cookie package.
Here’s an overview of working with cookies in Laravel:
Setting a Cookie: You can set a cookie using the cookie
helper function or the Response
instance. For example:
// Using the cookie helper
cookie('name', 'value', $minutes);
// Using the Response instance
$response = new Response('Hello');
$response->cookie('name', 'value', $minutes);
The cookie helper function accepts three arguments: name, value, and expiration time in minutes. Illuminate\Session
Getting a Cookie Value: You can retrieve the value of a cookie using the cookie
helper function or the Request
instance. For example:
// Using the cookie helper
$value = cookie(‘name’);
// Using the Request instance
$value = $request->cookie(‘name’);
Attaching Cookies to Responses:
When sending a response, you can attach cookies using the withCookie method on the Response instance. For example:
return response('Hello')->withCookie(cookie('name', 'value', $minutes));
Forgetting a Cookie:
To remove a cookie, you can use the forget method on the Response instance. For example:
return response('Hello')->withCookie(cookie()->forget('name'));
This will set the cookie with an expiration date in the past, effectively deleting it from the client-side browser.

I’m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I’ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains:
-
DevOps School – Tech blogs and tutorials
-
Holiday Landmark – Travel stories and guides
-
Stocks Mantra – Stock market strategies and tips
-
My Medic Plus – Health and fitness guidance
-
TrueReviewNow – Honest product reviews
-
Wizbrand – SEO and digital tools for businesses
I’m also exploring the fascinating world of Quantum Computing.
[…] https://www.devopsconsulting.in/blog/laravel-cookies-best-practices-implementation-guide/ […]