Hello there,
I tried so many times for maintaining cart persistence, but I failed. Even though I set the property destroy_on_logout to false in my config file, the cart is becoming empty on logout. Is there any solution for this?
Try this it worked for me .
public function destroy($id)
{
Cart::instance('default')->remove($id);
return redirect()->back()->with('success', '韦慰 蟺蟻慰蠆蠈谓 伪蠁伪喂蟻苇胃畏魏蔚 伪蟺慰 蟿慰 魏伪位维胃喂.');
}
Hey @jordantsap, My problem is not with removing the item from cart. The actual problem is, when the user logs out, the cart is becoming empty.
Sorry about that I should comment the usercontroller@destroy or logout function not the cartcontroller, I'll do it in a second
Check your logout function.
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate(); - this will flush all your sessions including cart.
return redirect('/');
}
public function logout(Request $request)
{
$cart = collect(session()->get('cart'));
if (!config('cart.destroy_on_logout')) {
$cart->each(function($rows, $identifier) {
session()->put('cart.' . $identifier, $rows);
});
}
return redirect()->back();
}
@jordantsap thanks buddy, it worked for me.
public function logout(Request $request)
{
$this->guard()->logout();
if (config('cart.destroy_on_logout')) {
$request->session()->invalidate();
}
$request->session()->regenerateToken();
if ($response = $this->loggedOut($request)) {
return $response;
}
return $request->wantsJson()
? new Response('', 204)
: redirect('/');
}
A solution that incorporates @jordantsap's solution cleanly has been explained here:
https://github.com/bumbummen99/LaravelShoppingcart/issues/94
Most helpful comment
public function logout(Request $request)
{
$cart = collect(session()->get('cart'));