我正在尝试制作带有复选框的表格,管理员可以在其中检查多个产品并删除它们。到目前为止我已经制作了表格
@foreach($products as $product)
{{ Form::open() }}
<input type="checkbox" name="delete[]" value="{{ $product->product_id }}">
<a class="btn btn-primary" href="{{ URL::to('/admin/products/multiDdelete') }}?_token={{ csrf_token() }}">Delete</a>
{{ Form::close() }}
@endforeach
Run Code Online (Sandbox Code Playgroud)
这是我的路线
Route::get ('/admin/products/multiDdelete', ['uses' => 'AdminController@testDelete', 'before' => 'csrf|admin']);
Run Code Online (Sandbox Code Playgroud)
这在控制器中
public function testDelete() {
$delete = Input::only('delete')['delete'];
$pDel = Product::where('product_id', $delete);
$pDel->delete();
return Redirect::to('/admin/test')->with('message', 'Product(s) deleted.');
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,当我检查产品并点击Delete
页面重新加载时,我的产品已被删除,但产品并未被删除。我认为问题在于我如何通过ID's
..但我无法弄清楚。
您的查询在这里没有返回任何有用的内容。即使使用->get()
,它也会返回一个集合,您无法按照您想要的方式使用它。您可以将删除添加到查询中:
Product::whereIn('product_id', $delete)->delete();
Run Code Online (Sandbox Code Playgroud)