Unsetting the product from your Request object isn't going to remove it from the session.
It looks like you might want to add a remove() method to your Cart class, and then update the session?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I stored the items into cart via sessions.
Add items into cart code:
Cart Class:
<?php
namespace App;
Class Cart
{
public $item = null ; //group of products
public $totalQty = 0;
public $totalPrice = 0;
public function __construct($oldCart)
{
//if card already exists
if($oldCart){
$this->item = $oldCart->item;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function add($item,$id)
{
$storedItem = [
'qty' => 0,
'price' => $item->price,
'item' => $item,
];
if($this->item){
if(array_key_exists($id, $this->item)){
$storedItem = $this->item[$id];
}
}
$storedItem['qty']++; //add each time qty
$storedItem['price'] = $item->price * $storedItem['qty'];
$this->item[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
}
Controller method that adds items into cart
// add to cart function
public function add_to_cart(request $request)
{
$product = Product::find($request->id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new cart($oldCart);
$cart->add($product,$request->id);
$request->session()->put('cart',$cart); //save into session
//$request->session()->get('cart');//for get what is in cart
$totalItems = Session::get('cart')->totalQty;
return response()->json(['success' => 'Added to cart!','totalItems' => $totalItems]);
}
View Showing all items in cart code:
@foreach($products as $key => $product)
<tr width="100%">
<td width="20%" class="text-center removeBtn"><img src="{{asset('frontEnd/images/delete-icn.svg')}}" data-product="{{$product['item'][$key]}}"></td>
<td width="20%"><img src="{{Storage::url($product['item']['cover'])}}" class="img-rounded"></td>
<td width="20%" class="text-center">{{$product['item']['product']}}</td>
<td width="20%" class="text-center">{{$product['price']}}</td>
<td width="20%" class="text-center">
<button class="plus-btn" type="button" name="button">
<img src="{{asset('frontEnd/images/plus.svg')}}" alt="" />
</button>
<input type="text" name="name" value="1" style="width:20%;height:5px;">
<button class="minus-btn" type="button" name="button">
<img src="{{asset('frontEnd/images/minus.svg')}}" alt="" />
</button>
</td>
</tr>
@endforeach
Remove Item method:
public function removeProductFromCart(Request $request)
{
$product = $request->product;
unset($request->product); // it removes items but temporary.when i refresh the page then the item is
// shown me.
return response()->json(['info' => 'Item Remove From Cart!']);
}
Please give me solution.pleaseeee
// Get id of product
$id = $request->product;
// Get the product array
$cart = Session::get('cart');
// Unset the first index (or provide an index)
unset($cart->item[$id]);
// Overwrite the product session
Session::put('cart', $cart);
Please or to participate in this conversation.