【问题标题】:POST method is not supported for this route此路由不支持 POST 方法
【发布时间】:2019-10-16 20:30:30
【问题描述】:

首先,我检查了其他问题主题,但找不到解决方案。

当我尝试发布我的表单时。我收到此错误。

此路由不支持 POST 方法。支持的方法: 得到,头。

表格:

<div class="card-body">
    <form action="{{route('profile.update', ['id' => $id])}}" method="post">
      @csrf
      @put

        <div class="form-group">
            <label for="location">Location</label>
            <input class="form-control" type="text" name="location" value="{{$info->location}}">
        </div>
        <div class="form-group">
            <label for="about">About</label>
            <textarea name="about" id="about" rows="10" cols="50" class="form-control">{{$info->about}}</textarea>
        </div>
        <div class="form-control">
            <p class="text-center">
                <button class="btn btn-primary btn-md" type="submit">Update Your Info</button>
            </p>
        </div>
    </form>
</div>

路线:

Route::group(["middleware" => "auth"], function(){
    route::get("/profile/edit", [
        "uses" => "ProfilesController@edit",
        "as" => "profile.edit"
    ]);
    route::get("/profile/{slug}", [
        "uses" => "ProfilesController@index",
        "as" => "profile"
    ]);
    route::put("/profile/update/{id}", [
        "uses" => "ProfilesController@update",
        "as" => "profile.update"
    ]);
});

在控制器中:

public function update(Request $request, $id)
{

    dd($request->all());

}

【问题讨论】:

  • 因为你没有使用资源路由,所以你应该从你的表单中删除这个{{ method_field('PUT') }}
  • 对于 REST 约定,更新资源时使用put http 动词
  • @parabellum 您是否要更新现有资源?
  • @parabellum 我更新了我的遮阳篷
  • @AdityaThakur 我正在尝试更新数据库中的配置文件表

标签: laravel


【解决方案1】:

根据您的问题,我可以理解您正在尝试使用 POST 方法更新配置文件,或者可能是更早的 PUT 方法。由于您正在编辑的资源是唯一的,因此您无需为控制器传递任何参数来查找单个资源以对其进行更新。

因此修改您的路线,例如

 route::put("/profile/update/{id}", [
        "uses" => "ProfilesController@update",
        "as" => "profile.update"
    ]);

和你的表格一样

<form action="{{route('profile.update', ['id' => $id])}}" method="post">
@csrf
@method('put')

您需要将要更新的配置文件的 ID 作为参数传递

然后在控制器处

public function update(Request $request, $id){
 //edit the profile with id = $id
}

【讨论】:

  • 我试过了,但它说:Undefined variable: id (View: /var/www/realestate/resources/views/profiles/edit.blade.php) 我也在底部的问题控制器中添加了
  • @parabellum 那是因为我输入的$id 只是模拟ID,您需要传递您正在编辑的配置文件的ID,在您的情况下,您将在您的视图中获取配置文件数据id 应该类似于 $info-&gt;id,
  • 哦,抱歉,这是我的错误,我已修复。但它仍然给出该方法错误
  • 能否用route::get("/profile/{slug}")评论方法,然后检查
  • 为什么,如果我注释掉那条路线,我就无法访问该页面?
【解决方案2】:

您的表单定义有误

 <form class="{{route('profile.update', ['id' => $id])}}" method="post">

应该是

 <form action="{{route('profile.update', ['id' => $id])}}" method="post">

【讨论】:

  • 哇大语法..好吧,它不工作..错误是The POST method is not supported for this route. Supported methods: PUT.
  • @parabellum 将 @put 更改为 @method('put'),我的错
  • @AdityaThakur 是的,改变解决了这个问题。谢谢。
【解决方案3】:

这是您提供的示例中的更正。

表格route('profile.update', ['id' =&gt; {here you have to place id of record which you want to update}])

查看文件 $info->id])}}" method="post">

        <div class="form-group">
            <label for="location">Location</label>
            <input class="form-control" type="text" name="location" value="{{$info->location}}">
        </div>
        <div class="form-group">
            <label for="about">About</label>
            <textarea name="about" id="about" rows="10" cols="50" class="form-control">{{$info->about}}</textarea>
        </div>
        <div class="form-control">
            <p class="text-center">
                <button class="btn btn-primary btn-md" type="submit">Update Your Info</button>
            </p>
        </div>
    </form>
</div>

在路上

Route::group(["middleware" => "auth"], function(){
    route::get("/profile/{slug}", [
        "uses" => "ProfilesController@index",
        "as" => "profile"
    ]);
    route::get("/profile/edit/profile", [
        "uses" => "ProfilesController@edit",
        "as" => "profile.edit"
    ]);
    route::post("/profile/update/profile/{id}", [
        "uses" => "ProfilesController@update",
        "as" => "profile.update"
    ]);
});

在控制器中

public function update(Request $request, $id)
{
    dd($id, $request->all());
}

【讨论】:

  • 您从表单中删除了method_field('PUT')。只有添加方法才会起作用
  • 用 POST 错误未放置和代码更新了问题
  • 我把输入隐藏在表单的底部并尝试过,但仍然是同样的错误。
  • @parabellum 立即查看。我用更正更新了你的代码。
【解决方案4】:

既然你为PUT请求做了一个表格,你必须改变

route::post("/profile/update/profile", [
    "uses" => "ProfilesController@update",
    "as" => "profile.update"
]);

到这里

route::put("/profile/update/profile", [
    "uses" => "ProfilesController@update",
    "as" => "profile.update"
]);

【讨论】:

  • 谢谢你,但如果它这么容易解决我不会问这个问题:) 没有效果...
  • 好吧,看看我的另一个答案,比这更容易:)
猜你喜欢
  • 2019-08-23
  • 2020-09-04
  • 1970-01-01
  • 2020-12-05
  • 2021-07-19
  • 2020-04-13
  • 1970-01-01
  • 2019-08-28
  • 2021-04-11
相关资源
最近更新 更多