Assuming they can add more than one thing to the cart, you'd maybe do something like :
// add an item to the cart
$request->session()->push('cart', $id);
// at checkout
$productIds = session('cart');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, sessions is a new thing for me. I'm making a cart now and thinking how for non-registered user add products to his cart. This is my solution for registered user:
public function add(Request $request, $id)
{
Cart::create([
'user_id' => Auth::id(),
'product_id' => $id,
'quantity' => 5
]);
return redirect()->back();
}
I'm grabbing it's ID and then displaying his products in cart by his ID. But how to put this code in session ?
Just find them in the db, I think just $products = Products::find($productIds) or whatever your products model is called.
Please or to participate in this conversation.