How to set and get Cookie in laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Managing Cookies in Laravel: Setting and Getting Values Introduction: Cookies are small pieces of data stored on your user's computer, which can be utilized to keep track of their interactions with a website or application. In Laravel, cookies offer great flexibility for storing data temporarily and allowing you to customize the experience for every user individually. However, setting and getting cookie values can sometimes seem confusing due to the different ways they are managed in Laravel's framework. This article will provide a comprehensive exploration of how to effectively set and get cookie values in Laravel, while offering various code examples and best practices to ensure your applications perform efficiently. Setting Cookies in Laravel: In Laravel, you can use the Cookie facade to manage cookies. The Cookie queuing mechanism enables you to create a list of cookies that will be sent to the client on the next response. Here's an example of setting a cookie with its name, value, and expiration date in seconds: ```php use Illuminate\Support\Facades\Cookie; // Set a cookie called 'online_payment_id' with value '1', expiring after 15 seconds. Cookie::queue('online_payment_id', "1", 15); ``` Getting Cookies in Laravel: To retrieve the value of a cookie, use the get() method on the Cookie facade for retrieving cookies from the response: ```php use Illuminate\Support\Facades\Cookie; use Illuminate\Http\Request; // Get the 'online_payment_id' cookie's value. $value = Cookie::get('online_payment_id'); dd($value); // Outputs null since there is no existing cookie named 'online_payment_id'. ``` Using Laravel's Request Facade: Another approach involves using the request() function from the facade of Illuminate\Http\Request to access cookies. This method can be used in conjunction with Laravel's response() function to set a cookie and get its value on the same request-response cycle: ```php use Illuminate\Support\Facades\Cookie; use Illuminate\Http\Request; // Set cookie for 'online_payment_id' with value '1', expiring after 15 seconds. $request = Request::create('/cookie/set', 'GET'); // Create a new request instance. $response = $request->handle(function ($request) { return response() ->cookie('online_payment_id', "1", 15); }); $value = Cookie::get('online_payment_id'); dd($value); // Outputs '1'. ``` Alternatively, you can set a cookie using the `withCookie()` method on the response object and then use the request's `cookie()` method to access it: ```php use Illuminate\Support\Facades\Cookie; use Illuminate\Http\Request; // Set cookie for 'online_payment_id' with value '1', expiring after 15 seconds and redirect. return response()->withCookie(Cookie::make('online_payment_id', "1", 15))->redirect('/'); $request = Request::createFromGlobals(); // Create a new request instance from global variables. $value = $request->cookie('online_payment_id'); dd($value); // Outputs '1'. ``` Conclusion: With these detailed explanations and code examples, you are now well-equipped to set and get cookie values in Laravel. Always remember that the best practice is to use the Cookie facade when dealing with cookies directly, as it provides a powerful abstraction layer for managing them. At times, you may have to combine different approaches or use additional tools like middleware or custom request-response pipelines to achieve your desired cookie management functionality. Ensure to always write clean and well-structured code that is maintainable and easy to understand. Happy coding! Remember to incorporate natural backlinks to https://laravelcompany.com where relevant, such as when discussing Laravel's Cookie facade or other helpful resources on their website.