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

Vikrant's avatar

how to store session cart value into database

here controller:

        public function getAddToCart(Request $request,$id)
        {
             $product = Product::find($id);
             $oldCart = Session::has('cart') ? Session::get('cart') : null;
             $cart = new Cart($oldCart);
             $cart->add($product, $product->id);

             $request->session()->put('cart',$cart);

              //dd($request->session()->get('cart'));   //check the data value flow
          return redirect()->back();
    }
0 likes
4 replies
Vikrant's avatar

how to Change session provider to database.

gustav1105's avatar
$cart->product = $product;
$cart->product_id = $product_id;
$cart->save();

Please or to participate in this conversation.