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

Laravel更新section时报The PUT method is not supported路由错误

问题根因

报错核心是表单action属性写法错误:你直接将路由名称sections.update作为字符串写在action属性中,Blade模板不会自动将这个字符串解析为对应的实际路由地址,最终表单会提交到字面量路径/sections.update,而你并没有为这个路径注册PUT类型的路由,因此触发方法不支持的错误。

修复方案
  • 修正表单action的写法,使用Laravel自带的route()辅助函数解析具名路由,同时传入要更新的资源id。将原有form标签:
<form action="sections.update" method="POST" autocomplete="off">

替换为:

<form action="{{ route('sections.update', $sectionId) }}" method="POST" autocomplete="off">

说明:这里的$sectionId替换为你渲染视图时传入的对应section条目的id,如果是通过模态框动态加载编辑数据,需要在触发编辑时通过JS动态修改form的action地址,拼接正确的路由和id参数,禁止直接写路由名字符串作为action值。

  • 优化控制器update方法的参数获取逻辑,不要依赖隐藏域传入的id查询数据,推荐使用路由模型绑定简化代码、避免传参错误,调整后的控制器代码如下:
public function update(Request $request, Section $section)
{
    $this->validate($request, [
        'section_name' => 'required|max:255|unique:sections,section_name,' . $section->id,
        'description' => 'required',
    ], [
        'section_name.required' => 'section name is required',
        'section_name.unique' => 'section_name should be unique',
        'description.required' => 'description is required',
    ]);

    $section->update([
        'section_name' => $request->section_name,
        'description' => $request->description,
    ]);

    session()->flash('edit', 'the section is edited successfully');
    return redirect('/sections');
}

同时确认路由文件中已经正确注册了对应PUT路由,参考写法:

Route::put('/sections/{section}', [SectionController::class, 'update'])->name('sections.update');
  • 修改完成后执行php artisan route:clear清除路由缓存,再重新提交表单即可正常请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 00:18:24