I want to set cookies in Laravel 5 independently
i.e., Don't want to use
return response($content)->withCookie(cookie('name', 'value'));
I just want to set cookie in some page and retrieve in some other page
Creation can be like this
$cookie = Cookie::make('name', 'value', 60);
But how can i retrieve those cookies in some controller itself ?
You are going right way my friend.Now if you want retrive cookie
anywhere in project just put this code $val = Cookie::get('COOKIE_NAME');
That's it!
For more information how can this done click here
Here is a sample code with explanation.
//Create a response instance
$response = new Illuminate\Http\Response('Hello World');
//Call the withCookie() method with the response method
$response->withCookie(cookie('name', 'value', $minutes));
//return the response
return $response;
Cookie can be set forever by using the forever method as shown in the below code.
$response->withCookie(cookie()->forever('name', 'value'));
//’name’ is the name of the cookie to retrieve the value of
$value = $request->cookie('name');
If you want to set cookie and get it outside of request, Laravel is not your friend.
Laravel cookies are part of Request, so if you want to do this outside of Request object, use good 'ole PHP setcookie(..) and $_COOKIE to get it.
Source: Stackoverflow.com