试图在 Laravel 中一次删除多条记录
Trying to delete multiple records at once in Laravel
我正在尝试使 table 带有复选框,管理员可以在其中选中多个产品并删除它们。到目前为止,我已经制作了表格
@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
这是我的路线
Route::get ('/admin/products/multiDdelete', ['uses' => 'AdminController@testDelete', 'before' => 'csrf|admin']);
控制器中的这个
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.');
}
到目前为止,当我检查产品并点击 Delete
页面重新加载时,我得到 产品已删除,但产品没有被删除。我认为问题在于我如何通过 ID's
.. 但我想不通。
您的查询return在这里没有任何用处。即使使用 ->get()
,它也会 return 一个集合,您不能按您想要的方式使用它。您可以改为将删除添加到查询中:
Product::whereIn('product_id', $delete)->delete();
我正在尝试使 table 带有复选框,管理员可以在其中选中多个产品并删除它们。到目前为止,我已经制作了表格
@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
这是我的路线
Route::get ('/admin/products/multiDdelete', ['uses' => 'AdminController@testDelete', 'before' => 'csrf|admin']);
控制器中的这个
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.');
}
到目前为止,当我检查产品并点击 Delete
页面重新加载时,我得到 产品已删除,但产品没有被删除。我认为问题在于我如何通过 ID's
.. 但我想不通。
您的查询return在这里没有任何用处。即使使用 ->get()
,它也会 return 一个集合,您不能按您想要的方式使用它。您可以改为将删除添加到查询中:
Product::whereIn('product_id', $delete)->delete();