Hi, how can i delete data from session. it do not want delete all data by clicking single delete button.
I have delete button for each data, so want to delete one by one. my current code is as follows:
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]);
}
}
//put back in session array without deleted item
$request->session()->push('product',$products);
//then you can redirect or whatever you need
return redirect()->back();
}
My data in session is in below format
[
{
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
}
]
In my network tab, i got following error when i clicked the delete button
{message: "Invalid argument supplied for foreach()", exception: "ErrorException",…}
exception: "ErrorException"
file: "/Applications/XAMPP/xamppfiles/htdocs/fresh/app/Http/Controllers/ProductController.php"
line: 33
message: "Invalid argument supplied for foreach()"
trace: [{file: "/Applications/XAMPP/xamppfiles/htdocs/fresh/app/Http/Controllers/ProductController.php",…},…]
0: {file: "/Applications/XAMPP/xamppfiles/htdocs/fresh/app/Http/Controllers/ProductController.php",…}
1: {function: "deleteProduct", class: "App\Http\Controllers\ProductController", type: "->"}
2: {,…}