Laravel version 5.5

This article explains how to save cookie data to a browser and show them to the fronted.

The controller – in our example we’ll show to to get a variable value and store it immediately to a cookie with a given name.

First things first get the cookie functionality we’ll need to import the  cookie facades and the cookieJar, at the top of your controller outside the class declaration, we’ll write:

use Illuminate\Cookie\CookieJar;
use Illuminate\Support\Facades\Cookie;

at the function declaration we send in the cookieJar, which looks like this:

public function example(CookieJar $cookieJar)

in that fuction depending on what we’re doing with the function and what it returns we store the cookie like this:

Cookie::queue(Cookie::make('SOME_NAME', $VARIABLE_NAME, 3000));

where SOME_NAME is the name of the cookie wich will be shown on the browser, and $variable_name the variable name of course which you want to store in the cookie.


After the cookie is saved there is just one think left, showing it on the fronted. using blade:

{{ Cookie::get('SOME_NAME') }}

Where SOME_NAME is the cookie name we declared while saving it from the controller.