php - laravel removing item from session -
i using sessions store cart items. able modify session items having trouble in removing them. function i'm stuck on
public function removecartitems(request $request) { $id = $request->input('id'); $items = session::get('cart.items', []); foreach ($items &$item) { if ($item['id'] == $id) { unset($item); } } //session::set('cart.items', $items); $request->session()->put('cart.items', $items); return 'removed'; }
this how add items in session
$item = array("id"=>"$productid", "name"=>"$name", "qty"=>"$qty", "rate"=>"$rate", "preview"=>"$preview", "front"=>"$front", "back"=>"$back"); $request->session()->push('cart.items', $item);
firstly, fyi, don't need wrap variables in " "
.
i suggest changing logic adding items cart using push()
put()
, use product id:
$item = [ "id" => $productid, "name" => $name, "qty" => $qty, "rate" => $rate, "preview" => $preview, "front" => $front, "back" => $back, ]; $request->session()->put('cart.items.' . $item['id'], $item);
then remove method like:
public function removecartitems(request $request) { $request->session()->forget('cart.items.' . $request->input('id')); return 'removed'; }
hope helps!
Comments
Post a Comment