1

i want to delete a row from stored session array. I could successfully delete it by sending the row number statically like in the code line below. But here i want to do it dynamically by passing the Array Row ID or line number. I want to replace the '0' with 'item_id' that is the array row number or id. How do i get that Id from array ?

$request->session()->pull('quoteSession.'.$id.'.vibrantScope.0');

This is my Array Console Log after deleting the first record using code above.

quoteSession:
  125:
    vibrantScope:
       1: "Array line 2"
       2: "Array line 3"
       3: "Array line 4"
       4: "Array line 5"

here is my Blade code


@foreach(Session::get('quoteSession.'.$lead->id.'.vibrantScope') as $item)
  <tr>
      <td scope="row">{{$loop->iteration}}</td>
      <td class="p-1 min-w-100" width="">
          {{$item}}
      </td>
      <td class="text-center">
          <a href="javascript:void(0);" onclick="removeScopeSession('{{$lead->id}}', '{{$item_id}}')" id="removeScope" title="Remove Scope" class="text-danger h6"><span class="fa fa-minus-circle"></span></a>                                        
      </td>
  </tr>
@endforeach

Here is my onClick Ajax call function to call delete controller

function removeScopeSession(lead_id, item_id){
    $.ajax({
        type: "post",
        url:"/scope/ajaxRemoveVibrantScopeSession",
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },        
        data:{
            lead_id : lead_id,
            item_id : item_id,
        },
        dataType: 'json', 

        success: function(response){
            console.log(response);
            $("#tblVibrantScope").load(" #tblVibrantScope > *");
            toastr.success(' ', 'Scope Removed', {timeOut: 3000, positionClass: 'toast-bottom-right'});
            },
        error: function(error){
            console.log(error);
            toastr.error(' ', 'Scope not Removed', {timeOut: 3000, positionClass: 'toast-bottom-right'});
        }
    });
}

Here is my controller function. here i want to replace the '0' with 'item_id' that is the array row number or id.

    public function ajaxRemoveVibrantScopeSession(Request $request)
    {
        $id = $request->lead_id;
        $item_id = $request->item_id;
        $delete_data = 'quoteSession.'.$id.'.vibrantScope.0';
        $request->session()->forget($delete_data);
        return $request->session()->all();
    }  
Thoufeeq
  • 11
  • 2

1 Answers1

0

Finally found the answer myself from stack.

get your array Key & value separated in your for loop with @foreach(Session::get('quoteSession.'.$lead->id.'.vibrantScope') as $key => $value) and then you can access them with $key and $value

$key will return the Key of array ie, [1]

$value will return the value of array ie, [Array line 2]

see the code below

@foreach(Session::get('quoteSession.'.$lead->id.'.vibrantScope') as $key => $value)
  <tr>
    <td scope="row">{{$loop->iteration}}</td>
    <td class="p-1 min-w-100" width="">
        {{$value}}
    </td>
    <td class="text-center">
       <a href="javascript:void(0);" onclick="removeScopeSession('{{$lead->id}}', '{{$key}}')" id="removeScope" title="Remove Scope" class="text-danger h6"><span class="fa fa-minus-circle"></span></a>                                        
    </td>
  </tr>
@endforeach
Thoufeeq
  • 11
  • 2