Jason Paddle Jason Paddle - 1 year ago 72
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() }}

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);

return Redirect::to('/admin/test')->with('message', 'Product(s) deleted.');

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

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download