[英]Laravel : Removing Array item from 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元素中刪除適當的元素,如何從會話數組中刪除特定的元素?
您需要執行以下操作:
unset
功能刪除該鍵。 再次將更新的數組設置為使用相同的鍵進行會話。
$product = Session::get('product'); //step 1 unset($product[$key]); //step 2 Session::put('product', $product); //step 3
如果product是您的會話數組,則使用以下代碼:
if(Session::has('product')){
foreach (Session::get('product') as $key => $value) {
if($value === $remove){
session()->forget('product.'.$key)
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.