Interested in sponsoring this lovely little spot?

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 }}
Yum yum yum.

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

{{ cookie: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 }}

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 }}

Setting

You can set a cookie with cookie:set:

{{ cookie:set 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" }}

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

{{ cookie:set likes="hats" :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" }}

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 prevent encryption you need to add an exception to the $except array in your app/Http/Middleware/EncryptCookies.php file.

Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →