Not sure what documentation you've been reading but take a look here:
Make and Get Cookies
Hi, im new on laravel. read documentation but not fix my problem.
how to set and get cookies on laravel.
here my sample code, not work yet.
routes.php
Route::get('add/{id}', 'HomeController@sample');
HomeController
use Request;
use Cookie;
class HomeController extends Controller {
public function sample($id)
{
Cookie::make('name', $id, 360);
return dd( Cookie::get('name') );
}
}
how make it work, and i want cookies globally on my frontend. thanks
@sitesense actually im still confused with the doc.
can you show me how to implement it on HomeController :)
Well the code you have used is never going to work because you're killing the response before it even reaches the browser.
Consider that the the cookie is stored in the browser and you dd() before it reaches it.
Read the cookie in the next request.
@sitesense still not work, what acctually i miss in my code.
using return Cookie::get('name') and return Request::cookie('name') still not return value.
routes.php
Route::get('add', 'HomeController@sample1');
Route::get('add/{id}', 'HomeController@sample2');
HomeController
class HomeController extends Controller {
public function sample1()
{
return Cookie::get('name');
}
public function sample2($id)
{
Cookie::make('name', $id, 60);
}
}
I can't test this right now and need to get to bed but try this :)
class HomeController extends Controller {
public function sample1()
{
return Cookie::get('name');
}
public function sample2($id)
{
Cookie::queue('name', $id, 60);
return response('Set cookie');
}
}
@sitesense thanks, it must use respone to set cookies to browser?
i check it on browser and cookies on there, the problem is when try return in on sample1 it still show blank.
Try:
Request::cookie('name');
still blank, i dont know why it returned blank and dont get value from cookies :(
@masandikdev the code below works, place it within your routes.php file. I have purposefully used different methods for interacting with cookies so you get the idea.
get('set-cookie', function() {
$response = new Illuminate\Http\Response('Hello World');
$response->withCookie(cookie()->forever('name-of-cookie', 'value-of-cookie')); // this will last five years
return $response;
});
get('get-cookie', function() {
dd(Cookie::get('name-of-cookie')); // showing you different ways to set / get the cookie
});
All you need to do is hit the URL /set-cookie in order to set it. This will set a cookie with the name name_of_cookie and the value value_of_cookie.
To retrieve it, just go to the URL /get-cookie where you will see a dump - "value of cookie".
@mstnorris it still show blank, i dont know why. :(
What version of Laravel are you using? To check run php artisan --version from within your project's root directory.
I just wrote the code I provided and I've tested it with:
Laravel Framework version 5.0.31
Laravel Framework version 5.1-dev
@sitesense @mstnorris oh i just realize why my cookies doesnt work because im add too many router order pattern on routes.php
i put it on last line after this route, that why it not passed :( like this :
Route::get('/', 'HomeController@index');
Route::get('{category}', 'HomeController@category');
Route::get('{category}/{id}-{title}', 'HomeController@single');
after i put @sitesense and @mstnorrisline code on first order, i got value from the cookies.
thanks @sitesense and @mstnorrisline :)
Why don't you use:
setcookie('name', 'some value', time()+60*60*24*365);
dd($_COOKIE['name']);
I can assure you, it will work.
Laravel is a Framework, not a religion.
You do not have to zealously use Laravel only tools inside of Laravel project.
Especially, if they are not helpful.
And in this case, when you want to set cookie and right away capture it and use it (right away, not in next request!), Laravel is not you going to help you.
Really dude? @jeffz2016 this was 2 years ago ffs.
@Jaytee How about now? How long is it FFS?
jeffz2016 +++ I spent the whole day to understand that the error lies in the fact that the cookies are not installed. Спасибо тебе чувак!!
Set a cookie like this:
Cookie::queue(cookie('key', 'value', $minute = 10))
Get a cookie like this:
request()->cookie('key')
this does for set cookie:
$response = new \Illuminate\Http\Response('Test');
$response->withCookie(cookie('test', 'test', 45000));
return $response;
and get cookie:
Cookie::get('test');
In Laravel 5.8 you can do:
Cookie::queue(Cookie::forget('name')); // Forget cookie (optional)
Cookie::queue('name',$value, $lifetime_in_minutes);
Please note that Cookie::queue does not accept a Carbon or TimeInterface as third parameter. It acceps only an int.
From your blade file or where ever you want to use
{{ Cookie::get('name', $default) }}
You can try this, worked for me
//Create:
cookie()->queue(cookie($name, $value, $minutes));
// forever
cookie()->queue(cookie()->forever($name, $value));
//get
request()->cookie($name);
//forget
cookie()->queue(cookie()->forget($name));
Please or to participate in this conversation.