11

How can I delete or destroy cookie in Laravel 4?

I have created it using the next code:

return Response::make('', 302, array('Location' => $_SERVER['HTTP_REFERER']))->withCookie($cookie);

But in the end, I need to destroy this data.

How can I do this?

3 Answers 3

24

You have to use Cookie::forget like this:

$cookie = Cookie::forget('cookieKey');

return Response::make('foo')->withCookie($cookie);

remember you have to always return the cookie with the response, it's not like Session.

2

Im pretty sure its the forget() method..

So in your case....when $cookie was made it was given a name, just unset it like so

Cookie::forget('yourCookie');
5
  • I have tried it like this (and it should work, since it is in documentation) but it won't work. In the end I did another Request to set up the same cookie with value of NULL. Not really the best solution but it works. Jun 19, 2013 at 21:19
  • 2
    While this is the right direction, Amir's answer below has the key piece of the puzzle. Cookie::forget() only makes a new cookie with null value and negative expiration--you still have to actually send it to the client to remove their cookie.
    – brism
    Oct 5, 2013 at 18:46
  • Crypt::setKey( Config::get('app.key') ); When using your own keys in crypting, reset the app.key first.
    – Joeri
    Jan 6, 2014 at 13:14
  • @VukStanković you may want to change the correct answer.
    – Amir
    Aug 9, 2014 at 13:48
  • 1
    Yes, I agree with @Amir
    – Kylie
    Aug 9, 2014 at 21:21
1

here is a list of frequently used with cookie here is a list of frequently used with cookies .
u could check the detail on Laravel Api document . [2]http://laravel.com/api/class-Illuminate.Cookie.CookieJar.html
wish u good luck !

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.