【问题标题】:The PATCH method is not supported for this route. Supported methods: GET, HEAD此路由不支持 PATCH 方法。支持的方法:GET、HEAD
【发布时间】:2020-08-06 17:22:18
【问题描述】:
<form action="{{ route('todo.edit',$todoedit->id,'edit') }}" method="POST" class="container">
        @csrf
        @method('PATCH')
        <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control" name="title" value="{{$todoedit->title}}">
        </div>
        <div class="form-group">
        <label for="description">Description</label>
        <input type="text" class="form-control" name="description" value="{{$todoedit->description}}">
        </div>
        <button type="submit" class="btn btn-primary form-control">Update</button>
        </form>

待办事项控制器:

    public function edit(Request $request,$id)
    { 
        $todo=Todo::find($id);
        $todo->title=$request->title;
        $todo->description=$request->description;
        $todo->save();
        return redirect(route('todo.index'));
    }

我不知道问题出在哪里,我正在做 CRUD,一切正常,但更新部分不起作用,它给了我错误

此路由不支持 PATCH 方法。支持的方法:GET、HEAD。

我已经尝试了所有方法,@method('UPDATE')PUT 以及所有方法,但它不起作用

【问题讨论】:

  • 请粘贴您的路线代码:web.php
  • web.php: Route::resource('todo', 'TodoController');

标签: laravel crud laravel-form


【解决方案1】:

因为您在控制器的编辑方法中编写了更新函数体。执行以下操作:

    public function update(Request $request,$id)
    { 
        $todo=Todo::find($id);
        $todo->title=$request->title;
        $todo->description=$request->description;
        $todo->save();
        return redirect(route('todo.index'));
    }

在您的编辑方法中,只需返回编辑视图并将$todo 对象传递给该对象

编辑方法类型为:Get

更新的方法类型是:PutPatch

您可以在终端中通过简单的运行php artisan route:list 查看这些类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-23
    • 2020-07-30
    • 2020-12-06
    • 1970-01-01
    • 2020-04-11
    • 2019-08-28
    • 2019-08-31
    相关资源
    最近更新 更多