no1uknow
no1uknow

Reputation: 549

Laravel Delete/Forget Cookie not working

How do you a delete cookies in Laravel. This is not working:

public function logout(Request $request)
{

    $this->guard()->logout();

    $request->session()->flush();

    $request->session()->regenerate();

    Cookie::queue(Cookie::forget('cavpad'));
    Cookie::queue(Cookie::forget('cavuser'));

    return redirect('/');

}

This works, but seems the wrong way to do it:

Cookie::queue(Cookie::make('cavpad', '', 0, null, env('APP_DOMAIN')));
Cookie::queue(Cookie::make('cavuser', '', 0, null, env('APP_DOMAIN')));

Why does the first way not work, but the second way does... btw, has nothing to do with the env()... Just added that in there...

Upvotes: 4

Views: 5426

Answers (2)

Julian Rodriguez
Julian Rodriguez

Reputation: 574

Try redirecting like this:

return redirect('/')->withCookie(Cookie::forget('cavpad'))->withCookie(Cookie::forget('cavuser'));

Upvotes: 1

FluxCoder
FluxCoder

Reputation: 1276

You can do this by using the code I provided, it's pretty much the same, but I know that this way of doing it works for me. But if you need to do it inline, this might also work for you:

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

This is how I think it should be done.

Upvotes: 1

Related Questions