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

tehseen's avatar

How to add whole cart object in to session.

I have a cart object but i want to add the whole cart in to session how i can do this kind of thing please advice.

cart object have the following values:

Cart {#346 ▼
  +items: array:3 [▶]
  +totalQty: 3
  +shippingCost: 4
  +totalPrice: 83.35
  +subTotal: 87.35
  +discount: 4.95
}

but i need to put this whole cart object in my session but the below code cannot work for me.

Session::put('subproducts', $cartobj);

the main aim is that need to put all items in my subproducts session.

Thanks in advance.

0 likes
1 reply
mushood's avatar

As per documentation: https://laravel.com/docs/5.7/session#storing-data

To store data in the session, you will typically use the put method or the session helper:

// Via a request instance... $request->session()->put('key', 'value');

// Via the global helper... session(['key' => 'value']);

so in your controller, you need to do this:

$request->session()->put('subproducts', $cartobj); OR
session(['subproducts', $cartobj]);

Please or to participate in this conversation.