Radical Design Course by Jack McDade

From the creator of Statamic

Learn how to make your websites standout and be remembered.

Bought Jack McDade's course on design. Going through it now...and it is SO well done!

— Justin Jackson, Transistor.fm

Cookie Tag

Cookies provide a client-side way to store information about the user across requests. The cookie tag will let you get, set, and forget cookie data.

You can use {{ cookie }} as a tag pair to access any cookie data that has been set.

{{ cookie }}
{{ oreo }}
{{ /cookie }}
{{-- Using Antlers Blade Components --}}
<s:cookie>
{{ $oreo ?? '' }}
</s:cookie>
 
{{-- Using the Cookie facade --}}
{{ Cookie::get('oreo') }}
Yum yum yum.

You can also retrieve single variables with a single tag syntax.

{{ cookie:oreo }}
{{-- Using Antlers Blade Components --}}
<s:cookie:oreo />
 
{{-- Using the Cookie facade --}}
{{ Cookie::get('oreo') }}

Checking

You can check if a cookie exists with cookie:has.

{{ if {cookie:has key="has_voted"} === true }}
You already voted. Thank you!
{{ /if }}
{{-- Using the Cookie facade --}}
@if (Cookie::has('has_voted') === true)
You already voted. Thank you!
@endif

Aliasing

If you need extra markup around your cookie data, you can alias a new child array variable.

{{ cookie as="snack" }}
{{ snack }}
{{ message }}
{{ /snack }}
{{ /cookie }}
{{-- Retrieving the message using the Cookie facade --}}
{{ Cookie::get('message') }}
 
{{-- Aliasing using Antlers Blade Components --}}
<s:cookie as="snack">
{{ $snack['oreo'] ?? '' }}
</s:cookie>

Setting

You can set a cookie with cookie:set:

{{ cookie:set my_key="my_value" }}
{{-- Using Antler Blade Components --}}
<s:cookie:set my_key="my_value" />
 
{{-- Using the cookie helper --}}
<?php
cookie()->queue(cookie('my_key', 'my_value'));
?>

You can optionally set a number of minutes the cookie is valid for (60 by default):

{{ cookie:set my_key="my_value" minutes="3600" }}
{{-- Using Antler Blade Components --}}
<s:cookie:set
my_key="my_value"
minutes="3600"
/>
 
{{-- Using the cookie helper --}}
<?php
cookie()->queue(cookie('my_key', 'my_value', 3600));
?>

You can set multiple cookies at once and reference interpolated data (references to variables).

{{ cookie:set likes="hats" :visited="url" }}
{{-- Using Antlers Blade Components --}}
<s:cookie:set
likes="hats"
:visited="$url"
/>
 
{{-- Using the cookie helper --}}
<?php
cookie()->queue(cookie('likes', 'hats'));
cookie()->queue(cookie('visited', $url));
?>
Hot Tip!

When you set a cookie, it will only be available on the next request.

Forgetting

You can remove cookies by passing the names of the variables into the keys parameter. Pass multiple keys by delimiting them with a pipe.

{{ cookie:forget keys="likes|referral" }}
{{-- Using Antlers Blade Components --}}
<s:cookie:forget
keys="likes|referral"
/>
 
{{-- Using the Cookie helper --}}
<?php
cookie()->queue(cookie()->forget('likes'));
cookie()->queue(cookie()->forget('referral'));
?>

Accessing Cookies in JavaScript

By default, in Laravel, cookies are encrypted so you will not be able to access the values of any data you set outside of PHP. To exclude specific cookies from encryption follow the steps below:

Laravel 10

To prevent encryption you need to add an exception to the $except array in your app/Http/Middleware/EncryptCookies.php file.

/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'cookie_name',
];

Laravel 11

To prevent encryption you need to use the encryptCookies method in your application's bootstrap/app.php file:

->withMiddleware(function (Middleware $middleware) {
$middleware->encryptCookies(except: [
'cookie_name',
]);
})
Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →