【发布时间】:2016-11-22 06:41:18
【问题描述】:
我有一个视图,其中显示了我的数据库中的所有产品,在该视图中我可以查看编辑和删除每个产品。
删除功能现在不能正常工作:
视图如下所示:
@foreach($products as $productKey => $productValue)
<tr>
<td>{{ $productValue->id }}</td>
<td>{{ $productValue->title }}</td>
<td>
@if($productValue->dr)
<a href="{{ route('editProduct', $productValue->id) }}" title="edit product"></a>
<form action="{{ route('destroyProduct') }}" method="post" name="delete_product_form">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $productValue->id }}">
<a href="#" title="delete" data-form="delete_product_form" data-action="submit"></a>
</form>
...
@endif
</td>
</tr>
@endforeach
我的销毁方法如下所示:
public function destroyProduct(Request $request)
{
$productID = $request->get('id');
$product = Product::find($productID);
$deleteFolder = "folderpath...";
if(is_dir($deleteFolder))
{
if(Helper::removeDirRecursive($deleteFolder))
{
if($product)
{
$product->delete();
return redirect()->route('indexProduct')->with('message', 'Success');
}
else
{
return redirect()->route('indexProduct')->with('message', 'Error');
}
}
}
elseif($product)
{
$product->delete();
return redirect()->route('indexProduct')->with('message', 'Success');
}
return redirect()->route('indexProduct')->with('message', 'Error');
}
目前它只是删除最新的产品,如果我回应的话
$product
我总是得到最新的产品。
我必须如何更改我的功能才能删除我正确选择的产品。
编辑:
我试过了,但还是不行:
<form action="{{ route('destroyProduct', $productValue->id) }}" method="post" name="delete_product_form">
编辑:
我的javascript提交函数:
observerSubmitButton: function() {
$('.observeSubmit').on('click', function() {
action = $(this).data('action');
formName = $(this).data('form');
if(action == 'submit')
{
$('form[name="'+formName+'"]').submit();
}
});
},
【问题讨论】:
-
如果您在创建它的行之后记录 productID 会返回什么?
-
最新id的字符串
"55" -
哦。 nvm我误会了,我以为是获取id的问题,但是你想要的id不应该是55对吗?
-
"55",一个字符串,没有数组,没有int