Laravel Batch Delete: Quick Guide

To achieve batch deletion in Laravel, you can follow these steps:

  1. Create a form to submit a list of IDs for multiple data items that need to be deleted. For example:
<form action="{{ route('items.delete') }}" method="post">
    @csrf
    @method('DELETE')
    
    @foreach($ids as $id)
        <input type="hidden" name="ids[]" value="{{ $id }}">
    @endforeach

    <button type="submit">删除选中项</button>
</form>
  1. Define a POST request in the routing to handle batch deletion operations. For example:
Route::delete('/items/delete', 'ItemController@batchDelete')->name('items.delete');
  1. Implement the corresponding method in the controller to handle batch deletion operations. For example:
public function batchDelete(Request $request)
{
    $ids = $request->input('ids');

    Item::whereIn('id', $ids)->delete();

    return redirect()->back()->with('success', '批量删除成功!');
}

This will enable batch deletion functionality in Laravel. It is important to make sure that the corresponding model has the SoftDeletes trait in order to achieve soft deletion.

bannerAds