尝试执行更新方法时,路由不支持 PUT 方法

PUT method not supported for the route when trying to do an update method

这就是我的代码的样子

路线:

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);


    }

Blade:

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

每次我尝试提交更新时,我都会得到:

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

我目前卡在这个上面了。

试试这个

<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');

最好这样做:

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

don't forget to use Request in controller

  • 在blade

最好使用路由命名,但这可能会影响您的操作

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

使用blade

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