php

Retrieving a Cookie from the Request

Problem

You want to check a cookie value sent to your application.

You know you could use the PHP $_COOKIE superglobal, but want to do it the Laravel way.

Solution

Use Cookie::get().

// $val will be null if cookie not present
$val = Cookie::get('COOKIE_NAME');

// Or you can pass a default, if not present
$name = Cookie::get('NAME', 'Unknown');
echo "Hello $name";

Discussion

This is basically the same as Request::cookie().

In fact, the Cookie::get() method is actually a wrapper over Request::cookie().

About the author

laravelrecipies