Backend Development 2 min read

Laravel Cookie Helper Cheat Sheet

This cheat sheet provides a concise reference for Laravel's Cookie helper functions, showing how to retrieve, create, queue, and delete cookies, as well as how to send cookies with responses and configure unencrypted cookies, complete with ready-to-use code examples.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Laravel Cookie Helper Cheat Sheet

Laravel includes a convenient Cookie helper class that simplifies setting, retrieving, and managing HTTP cookies within your application.

The following code snippets demonstrate common operations such as getting a cookie, creating permanent or temporary cookies, queuing cookies for the response, deleting cookies, and sending cookies directly with a response object. It also shows how to exclude specific cookies from encryption.

// 等于 Cookie
cookie();
request()->cookie('name');
Cookie::get('key');
Cookie::get('key', 'default');
// 创建一个永久有效的 cookie
Cookie::forever('key', 'value');
// 创建一个 N 分钟有效的 cookie
Cookie::make('key', 'value', 'minutes');
cookie('key', 'value', 'minutes');
// 在回应之前先积累 cookie,回应时统一返回
Cookie::queue('key', 'value', 'minutes');
// 移除 Cookie
Cookie::forget('key');
// 从 response 发送一个 cookie
$response = Response::make('Hello World');
$response->withCookie(Cookie::make('name', 'value', $minutes));
// 设置未加密 Cookie app/Http/Middleware/EncryptCookies.php
EncryptCookies->except = ['cookie_name_1'];
BackendphpCookieLaravelHelper
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.