【问题标题】:PUT method not supported for the route when trying to do an update method尝试执行更新方法时,路由不支持 PUT 方法
【发布时间】:2020-08-19 13:50:19
【问题描述】:

这就是我的代码的样子

路线:

Route::put('/articles/{$article}', 'ArticlesController@update');

控制器:

 public function update($id){

        $article=Article::find($id);

        $article ->title = request('title');
        $article->excerpt=request('excerpt');
        $article->body=request('body');

        $article->save();

        return redirect('/articles/'. $article->id);


    }

刀片:

 <form method="POST" action="/articles/{{$article->id}}" >
                @csrf
                @method('PUT')

每次我尝试提交更新时都会收到以下信息:

The PATCH method is not supported for this route. Supported methods: GET, HEAD.

我目前卡在这个上。

【问题讨论】:

  • 请分享与articles相关的所有路线。你在使用 resource() 路由吗

标签: php laravel laravel-6


【解决方案1】:

试试这个

<form action="/articles/{{$article->id}}" method="POST">
   <input type="hidden" name="_method" value="PUT">
   <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

路线

Route::put('/articles/{article}', 'ArticlesController@update');

【讨论】:

  • 谢谢,我没有注意到我在路线上有一个 $。我的坏
【解决方案2】:

最好这样做:

  • 在路由中
Route::put('/articles/{id}', 'ArticlesController@update')->name('articles.update');
  • 在控制器中
public function update(Request $request, $id)
{
   // logic
}

别忘了在控制器中使用 Request

  • 在刀片中

最好为路线使用命名,但这可能是您操作中的问题

   <form method="POST" action="{{ route('articles.update', $article->id) }}">

【讨论】:

    【解决方案3】:

    使用刀片的简单方法

    <form action="/articles/{{$article->id}}" method="POST">
     @method('put')
     @csrf
    </form>
    

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 2019-11-14
      • 2020-04-13
      • 1970-01-01
      • 2021-09-12
      • 2019-12-29
      • 2020-04-03
      • 2020-05-27
      • 1970-01-01
      相关资源
      最近更新 更多