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

david001's avatar

remove from session laravel and vue

Hi, i'm not able to delete data from the session. There are some data in the session . Now i want to remove one by one based on index value sent form ajax request.


//method to store in session, with ajax request
    public function store(Request $request) {
       
         $request->session()->push('product',json_decode($request->product));
         return session('product');
        
    }
//method to get data from session
    public function getFromSession(Request $request){
        
        $product = $request->session()->get('product');

        return $product;
       
    }
//delete method
    public function deleteProduct(Request $request){
       // dd($request->id);
   $request->id is the index value which starts from zero sent from view when user click remove button using 
     ajax request
 }

Route for delete


Route::get('/delete','ProductController@deleteProduct');

in view


//part of code
//getting data from session and looping 
      <div class="card-body" v-for="(list,index) in cart">
                        <img :src="list.image" width="80">
                       index:{{index}} {{list.name}}
                      

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

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

                     params: {
                        id: index,
                    }

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

                });
            }

Just what to know how to delete product based on delete from session and remove from page as well. Any help would be greatly appreciated

data in session: on dd($products) }

array:3 [▼
  0 => {#225 ▼
    +"id": 3
    +"name": "Ms. Clarissa Olson Jr."
    +"description": "Totam eius qui ut impedit nobis excepturi. Enim consequatur inventore dolorem laboriosam sequi dolore harum quibusdam. Doloribus ad tenetur tenetur et ut."
    +"category_id": 6
    +"price": 111
    +"image": "http://loremflickr.com/400/300?random=75"
    +"created_at": "2019-07-16 10:12:27"
    +"updated_at": "2019-07-16 10:12:27"
    +"qty": 1
  }
  1 => {#226 ▼
    +"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
  }
  2 => {#227 ▼
    +"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
  }
]
            }
0 likes
4 replies
tykus's avatar

Not 100% sure what you are doing, but the method for deleting from the session is forget, i.e.

$request->session()->forget('key');
david001's avatar

@tykus hi i have updated, i want to delete one product at a time based on button clicked. if user clicked remove button for item with index 0, item with index 0 should be removed, similarly if user clicked remove button for item with index1, item with index 1 should be removed

tykus's avatar

Ok, so you can use dot-syntax to target the top-level key and nested key, e.g. product.0

$request->session()->forget('product.' . $id)
1 like
david001's avatar

when i do not refresh the page, the product are deleted on clicking delete button from session. but when i click delete button for one product and refresh the page, and try to remove another product by clicking the delete button, product is not deleted


public function deleteProduct(Request $request){
       // dd($request->id);
        $id = $request->id;
        return $request->session()->forget('product.' . $id);
}

Does index value changes with that of session data when i refresh the page?

Please or to participate in this conversation.