Jason Paddle Jason Paddle - 1 month ago 9
PHP Question

Trying to delete multiple records at once in Laravel

I'm trying to make table with checkboxes where admin can check multiple products and delete them. So far I've made the form

@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


This is in my route

Route::get ('/admin/products/multiDdelete', ['uses' => 'AdminController@testDelete', 'before' => 'csrf|admin']);


And this in the controller

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.');
}


So far when I check products and hit
Delete
page reload and I get Product(s) deleted but products aren't deleted. I think the problem is in how I pass
ID's
.. but I can't figured it out.

Answer

Your query isn't returning anything useful here. Even with ->get(), it would return a collection, which you can't use the way you want. You can add delete to your query instead:

Product::whereIn('product_id', $delete)->delete();
Comments