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

如何在<x-app-layout>组件中整合数据表与表单?Blade组件技术疑问

Blade组件Items模块视图问题解决方案

1. 正确在内包裹自定义HTML并设置header插槽

直接通过<x-slot name="header">替换布局的头部插槽内容,主体自定义代码放在组件标签内部即可:

<x-app-layout>
    <!-- 自定义header插槽 -->
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            物品列表
        </h2>
    </x-slot>

    <!-- 主体内容区域 -->
    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <!-- 你的自定义HTML(比如表格、按钮等)放在这里 -->
        </div>
    </div>
</x-app-layout>

2. 遍历$items填充表格并添加删除按钮表单

使用@foreach遍历集合,删除按钮需用表单配合@method('DELETE')模拟RESTful删除请求,同时必须带上@csrf:

<table class="min-w-full divide-y divide-gray-200">
    <thead class="bg-gray-50">
        <tr>
            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">物品名称</th>
            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">操作</th>
        </tr>
    </thead>
    <tbody class="bg-white divide-y divide-gray-200">
        @foreach($items as $item)
            <tr>
                <td class="px-6 py-4 whitespace-nowrap">{{ $item->id }}</td>
                <td class="px-6 py-4 whitespace-nowrap">{{ $item->name }}</td>
                <td class="px-6 py-4 whitespace-nowrap">
                    <!-- 删除表单 -->
                    <form action="{{ route('items.destroy', $item) }}" method="POST" class="inline-block">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="text-red-600 hover:text-red-900" onclick="return confirm('确定删除该物品?')">
                            删除
                        </button>
                    </form>
                </td>
            </tr>
        @endforeach
    </tbody>
</table>

注意:确保路由items.destroy已正确定义,且传入$item模型实例时路由能自动解析ID。

3. 避免创建表单419 Page Expired错误

这个错误是CSRF验证失败导致的,解决方法很直接:

  • 在表单开头添加@csrf指令,它会自动生成含有效令牌的隐藏输入框
  • 确保表单使用POST方法(如果是PUT/PATCH/DELETE请求,还要配合@method)

示例代码:

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            创建新物品
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    <form action="{{ route('items.store') }}" method="POST">
                        @csrf <!-- 必须添加这一行 -->

                        <div class="mb-4">
                            <label for="name" class="block text-gray-700 text-sm font-bold mb-2">物品名称</label>
                            <input type="text" name="name" id="name" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" required>
                        </div>

                        <!-- 其他表单字段... -->

                        <div class="flex items-center justify-between">
                            <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
                                创建物品
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>

如果仍出现错误,可检查服务器session配置是否正常,或清理应用缓存。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.02 01:24:54