1

I'm using ajax to post data to a controller. In this controller I would like to set a cookie - I am then trying to read the set cookie (for testing purposes).

\Cookie::make('test', 'test-value', 2);
var_dump(\Request::cookie('test'));
die();

But the above results in a null value. Where am I going wrong?

Also on a side note - is it possible to store an array of values in a cookie. Is there any limit on the size of the array?

1
  • FYI, laravel has a method which does your var_dump and die for you so you could do dd($value) instead
    – haakym
    Apr 27, 2015 at 16:43

1 Answer 1

2

\Cookie::make() doesn't send the cookie to the browser of the user. It just creates the cookie.

If you want to set a cookie, use $response->withCookie(cookie('name', 'value', $minutes));. (For more information, look here: http://laravel.com/docs/5.0/requests#cookies)

Also calling \Request::cookie() after setting a new one will not display the new cookie because the new cookie wasn't send with the last request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.