Laravelshoppingcart: Cart is becoming empty on logout

Created on 5 Jun 2018  路  8Comments  路  Source: Crinsane/LaravelShoppingcart

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?

Most helpful comment

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();
}

All 8 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sobhanattar picture sobhanattar  路  4Comments

wasid picture wasid  路  8Comments

hatamiarash7 picture hatamiarash7  路  6Comments

ellgibug picture ellgibug  路  5Comments

abdullahkaamil picture abdullahkaamil  路  3Comments