Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ncarreno's avatar

Shopping cart and session id that change after login

I have a shopping cart where I save the session id, the item's id, qty, price and tax. It is work well but Laravel when I login Laravel change my session id and I have to add to my shopping cart all the items again. I am using Session::getId(); to get the user's session and then I store it in the database with the rest of data. What should I do to sort it out?

This is how I add and item to my cart:

    public function store(Request $request)
    {
        $data = $request->all();
        $numpro=count($data['product_id']);
        $session = \Session::getId();

        for ($i=0;$i<$numpro;$i++) {
            if ($data['qty'][$i]>0) {
                $product= Product::find($data['product_id'][$i]);

                $cartdata = Cart::where('session', $session)->where('product_id', $data['product_id'][$i])->first();

                if ($cartdata== null) {
                    $cart = new Cart([
                        'session'   =>  $session,
                        'product_id'    =>  $data['product_id'][$i],
                        'qty'   =>  $data['qty'][$i],
                        'price' =>  $product['price'],
                        'tax'   =>  $product['tax']
                    ]);

                    $cart->save();
                } else {
                    $cartdata->qty= $cartdata->qty+$data['qty'][$i];
                    $cartdata->save();
                }
            }
        }
0 likes
0 replies

Please or to participate in this conversation.