Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookie::forget not working laravel 5.1

I'm trying to get Laravel 5.1 to delete my cookie, however it will not delete even though i'm returning it with my redirect.

return redirect('/voucher')->withCookie(Cookie::forget($cookie));

Have I done something wrong?

like image 596
V4n1ll4 Avatar asked Aug 11 '15 12:08

V4n1ll4


People also ask

How do I enable cookies in laravel?

Show activity on this post. $cookie = cookie('name', 'value', $minutes); return response('Hello World')->cookie($cookie); Retrieving Cookies From Requests you can use Request be sure you use Request $request in you method. Show activity on this post.

How many arguments cookie () method will take in laravel?

Cookie() method will take 3 arguments. First argument is the name of the cookie, second argument is the value of the cookie and the third argument is the duration of the cookie after which the cookie will get deleted automatically.

How do you check if cookies are set or not in laravel?

Forum Check if cookie exists and is not null Last updated 3 months ago. You can get cookies from request: $value = Request::cookie('name', $defaultValue); if (Request::hasCookie('phone')) { ... }


2 Answers

Maybe I am wrong, but you are probably using cookie object in place of cookie name when calling Cookie::forget($cookie). Unless $cookie is a string containing cookie name, you should try something like this:

return redirect('/voucher')->withCookie(Cookie::forget('cookie_name'));
like image 66
Jan.J Avatar answered Sep 28 '22 03:09

Jan.J


I know this is already an old and answered question but I got here recently and if I'm correct, the cookie needs to be 'queued' for the next response.

You can do that by adding the cookie manually to the response as @Jan.J already described in his answer. But if you need to do it inline, this might also work for you:

Cookie::queue(
    Cookie::forget('cookieName')
);

The CookieJar will pass all queued cookies to the next response.

like image 28
Robin van Baalen Avatar answered Sep 28 '22 03:09

Robin van Baalen