我如何从laravel中的会话中删除数据。我不想通过单击单个删除按钮来删除所有数据。我有每个数据的删除按钮,所以想一个一个地删除。我当前的代码如下:
<a style="cursor: pointer;color: #ffffff;" (click)="sort()" class="text-nowrap">
<ng-content></ng-content>
<span *ngIf="isSortedByMeAsc" class="glyphicon glyphicon-triangle-top" aria-hidden="true"></span>
<span *ngIf="isSortedByMeDesc" class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></span>
</a>
我也尝试过
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
}
]
在dd()上
{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: {,…}
在我的网络标签中,我得到了
$id = $request->productId;
$products = $request->session()->get('product');
dd($products);
答案 0 :(得分:0)
$request->session()->forget('product');
将从会话中删除整个密钥,而您不希望这样做。所以...
如何从laravel中的会话中删除数据。
您的情况是:
public function deleteProduct(Request $request){
$id = $request->productId;
// Get the array from session.
$products = $request->session()->get('product');
// Remove unwanted key from array.
foreach ($products as $key => $value)
{
if ($value->id == $id)
{
unset($products[$key]);
}
}
// Override the session.
$request->session()->put('product', $products);
return redirect()->back();
}
例如,如果有一天在系统中添加了deleteAllProducts()
功能,那么最好致电$request->session()->forget('product');
答案 1 :(得分:0)
尝试此代码
$request->session()->forget('product');
答案 2 :(得分:-2)
不能将foreach用于数组,而应使用for循环
for($x = 0;$x<sizeOf($products);$x++)
{
$product = $products[$x];
#then do your manipulation here
}