david001's avatar

Delete multiple records using checkbox

How to perform multiple record deletion using checkbox to check all records and delete by clicking a delete button. route

Route::resource('case','CaseController');

controller //currently i have only one record delete at a time

 public function destroy($id)
 {
$case= Case::find($id);

$case->delete();
 }

view

    <p><label><input type="checkbox" id="checkAll"/> Check all</label>and<a href="">Delete all checked</a></p>

 @foreach ($cases as $case)
 <input type="checkbox" name="checkbox[]" data-id="checkbox"class="cb" value="{{$case->id}}" /> 
 @endforeach

and js

<script type="text/javascript">
  $("#checkAll").change(function () {
    $("input:checkbox").prop('checked', $(this).prop("checked"));
});
</script>


0 likes
3 replies
Corez64's avatar

The Eloquent find method actually supports passing it an array. You could easily do something like this to your destroy method in your controller:

public function destroy($id)
{
    Case::find(explode(',', $id))->delete();
}

Then send a HTTP DELETE /case/1,2,4,8,16 and it will delete the lot. You got to love Eloquent sometimes :)

Corez64's avatar

You may still need to explode the $ids passed in through the controller method parameter with @bobbybouwmann solution as you can't pass arrays list this to my knowledge.

Please or to participate in this conversation.