如何从 Laravel 的会话中删除数据。我不想通过单击单个删除按钮来删除所有数据。我对每个数据都有删除按钮,所以想一一删除。我目前的代码如下:
public function deleteProduct(Request $request){
$id = $request->productId;// this id i want to delete which i get from view
$products = $request->session()->get('product');
foreach ($products as $key => $value)
{
if ($value['id'] == $id)
{
unset($products [$key]);
}
}
//then you can redirect or whatever you need
return redirect()->back();
}
我也试过这个
foreach ($products as $product)
{
if ($product->id == $id)
{
$products->forget($product);
}
}
我在会话中的数据格式如下
[
{
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: 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
},
{
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
}
]
互换的青春
蝴蝶不菲
繁星coding