Laravel:从Session中删除Array项

时间:2017-09-09 16:50:13

标签: php laravel session laravel-5

我试图从Session中删除数组项。我按照以下方式展示了数组元素:

<?php $i=1; ?>

@foreach(Session::get('product') as $row)
    <tr>
        <td>
            <img src="{{asset('files/'.$row->thumbnil)}}" class="img-thumbnail" alt="" width="90px">
        </td>
        <td>{{$row->name}}</td>
        <td>
            <a href="{{asset('deleteEnquote/'.$row->id)}}">  
                <button class="btn btn-danger btn-sm"><i class="fa fa-remove"></i></button>
            </a>
        </td>
    </tr>
<?php $i++; ?>  
@endforeach

这就是我试图删除关键元素的方式:

public function deleteEnquote($id)
{

    $remove = Product::where('id',$id)->first();

    if(Session::has('product')){
        foreach (Session::get('product') as $key => $value) {
            if($value === $remove){
                Session::pull('product.'.$key); // retrieving pen and removing
                break;
            }
        }
    }

    return redirect('enquote');
}

但问题是我无法从未删除的Array.Means元素中删除相应的元素。如何从Session Array中删除特定元素?

2 个答案:

答案 0 :(得分:0)

你需要做这样的事情:

  1. 获取阵列。
  2. 使用 <div class="container" id="RappelPres" style=" width: 400px; height: 200px; position: absolute; top: 280px; left: 500px; border: 1px #a5a3a2 solid;"> <div style="background-color: #42b43b;width:100%;height:40px;position: absolute ;left:0px"> <form method="POST" action="{{ url('new') }}" style="text-align:center;" role="form" id="frm" > {{ csrf_field()}} <button class="btn" id="confirm" type="submit" style="background-color:#42b43b;color: white;width:100%;height:100% "></button> </form> </div> <br> <br> <hr> <div class="container" style="width:98%"> <div class="alert alert-success alert-dismissible hidden"> Pésence marqué avec succès. </div> </div> <div class="alert alert-danger alert-dismissible hidden" role="alert" style="width:98%"> </div> </div> 功能删除该密钥。
  3. 将更新的数组再次设置为具有相同键的会话。

    unset

答案 1 :(得分:0)

如果产品是您的会话数组,请使用以下代码:

if(Session::has('product')){
  foreach (Session::get('product') as $key => $value) {
    if($value === $remove){
      session()->forget('product.'.$key)
      break;
    }
  }
}
相关问题