You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 8中如何通过控制器删除视图中勾选的表格行数据?

Laravel 8 实现表格勾选行批量删除方案

1. 调整视图代码

替换原有视图代码,主要优化了表单结构,去掉每行冗余的隐藏删除表单,新增全局批量删除表单,同时保留原有单行操作能力:

@extends('layouts.app')
@section('content')
<body>
    <!-- 全局批量删除表单 -->
    <form action="{{ route('categories.bulkDelete') }}" method="POST" id="deleteForm">
        @csrf
        @method('DELETE')
        <button class="btn btn-danger" type="submit" onclick="return confirm('确定删除选中项吗?')">删除选中</button>
        
        <table class="table">
            <thead>
                <tr>
                    <th scope="col">
                        <input type="checkbox" id="selectAll">
                    </th>
                    <th scope="col">id</th>
                    <th scope="col">title</th>
                    <th scope="col">Paragraph</th>
                    <th scope="col">操作</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($categories as $categorie)
                <tr>
                    <td>
                        <input type="checkbox" class="rowCheckbox" name="ids[]" value="{{ $categorie->id }}">
                    </td>
                    <th scope="row">{{ $categorie->id }}</th>
                    <td>{{ $categorie->Title }}</td>
                    <td>{{ $categorie->Paragraph }}</td>
                    <td>
                        <!-- 保留原有单行删除/编辑功能 -->
                        <form action="{{ route('categorie.destroy', $categorie->id) }}" method="POST" class="d-inline">
                            @csrf
                            @method('DELETE')
                            <button class="btn btn-danger" type="submit" onclick="return confirm('确定删除该条数据吗?')">删除</button>
                        </form>
                        <a href="{{ route('edit', $categorie->id) }}" class="btn btn-warning">编辑</a>
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </form>

    <script>
        // 全选/取消全选逻辑
        document.getElementById('selectAll').addEventListener('change', function() {
            const checkboxes = document.querySelectorAll('.rowCheckbox');
            checkboxes.forEach(checkbox => checkbox.checked = this.checked);
        });

        // 单个复选框状态联动全选框(可选优化)
        const rowCheckboxes = document.querySelectorAll('.rowCheckbox');
        rowCheckboxes.forEach(checkbox => {
            checkbox.addEventListener('change', function() {
                const allChecked = Array.from(rowCheckboxes).every(cb => cb.checked);
                document.getElementById('selectAll').checked = allChecked;
            });
        });
    </script>
</body>
@endsection

2. 注册批量删除路由

在 routes/web.php 文件中添加以下路由,原有路由无需修改:

use App\Http\Controllers\CategoryController;

Route::delete('/categories/bulk-delete', [CategoryController::class, 'bulkDelete'])->name('categories.bulkDelete');

3. 控制器添加批量删除方法

在你的 CategoryController.php 中新增 bulkDelete 方法:

public function bulkDelete(\Illuminate\Http\Request $request)
{
    // 验证提交的ID数组合法性
    $request->validate([
        'ids' => 'required|array',
        'ids.*' => 'exists:categories,id'
    ]);

    // 批量删除选中数据
    \App\Models\Category::destroy($request->ids);

    // 返回原页面并携带成功提示
    return back()->with('success', '选中数据已删除成功');
}

可选优化:添加操作提示

在视图合适位置添加以下代码,可显示删除成功提示:

@if(session('success'))
    <div class="alert alert-success">{{ session('success') }}</div>
@endif

内容的提问来源于stack exchange,提问作者Souhaib Haydar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 10:36:03