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

Laravel 10中Blade组件表单使用PUT方法无法提交问题

问题排查与解决方案

核心问题:组件action参数的语法错误

你在调用表单组件时,action参数的写法存在错误:

@component("organisms.form", ["action" => "/admin/category/{{$category->id}}", "method" => "PUT"])

在Blade组件的属性数组中,不能嵌套使用{{ }}语法,这种写法会被Blade解析为HTML实体,最终生成的action路径会变成/admin/category/%7B%7B$category-%3Eid%7D%7D,而非正确的/admin/category/[分类ID],导致请求发送到错误路由,自然无法正常提交。

修正步骤

  1. 修正组件的action参数传递
    直接用PHP字符串拼接或Laravel命名路由传递正确路径:

    // 方式1:字符串拼接
    @component("organisms.form", ["action" => "/admin/category/{$category->id}", "method" => "PUT"])
    
    // 方式2:使用命名路由(推荐,更健壮)
    @component("organisms.form", ["action" => route('admin.category.update', $category), "method" => "PUT"])
    

    (需确保路由文件中已定义对应PUT路由,例如:Route::put('/admin/category/{category}', [CategoryController::class, 'update'])->name('admin.category.update');)

  2. 优化表单组件的方法处理(可选)
    目前组件里硬写了@method("put"),可改成根据传入的method参数动态生成,让组件更通用:

    // views/organisms/form.blade.php
    <form action="{{ $action }}" method="POST">
      @csrf
      @if($method !== 'POST')
        @method(strtoupper($method))
      @endif
    
      {{-- Error Messages --}}
      @if ($errors->any())
        @include("molecules.form-error-message", ["errors" => $errors])
      @endif
    
      {{-- Fields --}}
      {{ $fields }}
    
      {{-- Buttons --}}
      {{ $buttons }}
    </form>
    

验证要点

  • 修正后查看页面源代码,确认form的action属性是正确的分类ID路径,且存在<input type="hidden" name="_method" value="PUT">隐藏字段
  • 打开浏览器开发者工具的Network标签,确认提交请求的URL和请求参数(实际为POST请求,包含_method=PUT)是否正确

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 05:42:33