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

david001's avatar

How to delete individual data from session

Hi, i have data in session in below format. i want to delete the data based on id. So how my method should be in order delete data according id.

This is data in session


[
{
id: 5,
name: "Prof. Iliana Mohr",
description: "Autem sequi esse laudantium ut ut explicabo enim. Corporis cupiditate dolorum et ratione sequi architecto. Vitae enim ex hic nihil.",
category_id: 2,
price: 207,
image: "http://loremflickr.com/400/300?random=99",
created_at: "2019-07-16 10:12:27",
updated_at: "2019-07-16 10:12:27",
qty: 1
},
{
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: 1,
name: "Keyshawn McDermott Sr.",
description: "Error aut quia id dolorem est aut doloribus nesciunt. Quod nihil tenetur ea id voluptas molestias id. Debitis amet dolor est fugiat sed autem.",
category_id: 1,
price: 59,
image: "http://loremflickr.com/400/300?random=36",
created_at: "2019-07-16 10:12:27",
updated_at: "2019-07-16 10:12:27",
qty: 1
}
]

for example when user click the delete button in view, i want to delete record with id 5.

method:


public function deleteProduct(Request $request){
        $id = $request->productId;// this gives id that is submitted from form/link
        $request->forget($id);// this delete all records
    //i want to delete only one record which user wants/clicked.
}

0 likes
5 replies
Khosmoe_2019's avatar

Can you take a look at my recent post? I have an issue I'm trying to fix ASAP

Snapey's avatar

Load the array, pop the value and resave back to session.

But, how is it saved? Looks like you present it here as json. Is it in that format? How did you save it?

david001's avatar

@snapey i have looped the array in view(@foreach loop ), i have delete button for each data, i can get individual id in controller when i clicked the delete button. Now i want to delete based on those id. will you please give me help by providing little bit code help.

Snapey's avatar

cant help if I know how you put the data in session? (as i said before)

david001's avatar

Hi @snapey //this method push in session


public function StoreInCart(Request $request) {
     
         $request->session()->push('product',json_decode($request->product));
         return session('product');
        
    }

//this method fetch from session


    public function listProduct(Request $request){
        $product = $request->session()->get('product');
        return $product;

      
    }

I want to delete with Ajax request. For now how can i delete with Laravel only

Please or to participate in this conversation.