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

david001's avatar

Remove from session

Hi guys, i have a problem in remove the items from session when user click the remove item button from the session. Let me divide my problem in two parts

1First: There are certain items in cart, if i click remove item button, items are removed from session based on index value sent by ajax request. In this case i have not refresh the page that display the products.

2 Second. There are certain items in cart, if i click remove item button for any product, the product is now removed from session and their are remaining products. Now if i refresh web page, their index value decreased by one since i already removed one item from session. Now if i click remove button, some of the product are not removed. Can anybody suggest me what actually happening here. Thanks

Part of vue code


 <div class="card-body" v-for="(list,index) in cart">
                        <img :src="list.image" width="80">
                       index:{{index}} {{list.name}}
                      

                        <button @click="remove(index)">X</button>
             </div>


            remove(index){
                axios.get('/delete',{

                     params: {
                         index: index,
                    }

                }).then((response)=>{
                //this.cart123.push(product)

                });
            }

This is the method to remove Item from session

public function removeFromSession(Request $request){
      
        $index = $request->index;//index value of item sent from view when user clicks remove item

        $request->session()->forget('product.' . $index);
    

      }

Data stored in session as


[
{
id: 2,
name: "Marlene Reichert",
description: "Debitis asperiores sed sit assumenda unde quo natus. Consequatur est labore tenetur quae. Eius distinctio ea omnis aspernatur porro earum quae.",
category_id: 3,
price: 76,
image: "http://loremflickr.com/400/300?random=71",
created_at: "2019-07-16 10:12:27",
updated_at: "2019-07-16 10:12:27",
qty: 1
},
{
id: 4,
name: "Bart Kuvalis",
description: "Eaque iure excepturi nisi est dolore sapiente in sequi. Minus odit ab et id consectetur sunt.",
category_id: 9,
price: 162,
image: "http://loremflickr.com/400/300?random=40",
created_at: "2019-07-16 10:12:27",
updated_at: "2019-07-16 10:12:27",
qty: 1
},
{
id: 6,
name: "Kaylin Emard",
description: "Et aperiam omnis nam iure id non fugiat. Excepturi voluptatem ipsam magnam. Esse asperiores ducimus enim et.",
category_id: 8,
price: 14,
image: "http://loremflickr.com/400/300?random=17",
created_at: "2019-07-16 10:12:27",
updated_at: "2019-07-16 10:12:27",
qty: 1
}
]
0 likes
3 replies
rameezisrar's avatar

@david001 1) After removing the product from the session you need to get a fresh copy of session products to re-render them.

  1. Or you get the session products and store that in the data. Add/remove the products on the user product action and later send the axios post request with the data
david001's avatar

Hi can you help me with the code. Thanks for reply

Please or to participate in this conversation.