下面的代码用于删除复选框中所有选中的类别,但是,我为每个复选框都有一个隐藏的输入字段,其中包含用作缩略图的图像的文件名,并且由于我不想进行查询获取文件名,我将其作为隐藏输入传递。
因此$request['categoryFiles']
包含每个$request['categories']
的文件名,但是,我不确定如何在foreach
中使用该数组,以便我可以填充Storage::delete('public/uploads/categories/'.???);
具有$request['categoryFiles']
数组的正确索引。
???只要$request['categoryFiles'][0]
循环播放,就必须用$request['categoryFiles'][1]
,foreach
之类的东西替换
public function deleteCategories(Request $request){
if (!Auth::user()->hasRole('Admin')) {
return redirect()->back();
} else if (Auth::user()->hasRole('Admin')) {
$categories = $request['categories'];
$categoryFiles = $request['categoryFiles'];
foreach ($categories as $category) {
Storage::delete('public/uploads/categories/'.???);
Storage::delete('public/uploads/categories/thumbnails/'.???);
}
Category::whereIn('id', $categories)->delete();
return redirect()->back();
}
}
答案 0 :(得分:2)
您可以使用类别中的键与另一个数组进行匹配...
foreach ($categories as $key => $category) {
Storage::delete('public/uploads/categories/'.$categoryFiles[$key]);
Storage::delete('public/uploads/categories/thumbnails/'.$categoryFiles[$key]);
}