该路由不支持POST方法.支持的方法:GET,HEAD.拉拉韦尔

该路由不支持POST方法.支持的方法:GET,HEAD.拉拉韦尔

问题描述:

我的编辑页面有问题.提交时出现此错误:

I have a problem with my edit page. When I submit I get this error:

此路由不支持POST方法.支持的方法:GET,HEAD.

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

我不知道它来自哪里,因为我对Laravel还很陌生.

I have no clue where it comes from as I am pretty new to Laravel.

routes(web.php):

routes(web.php):

Route::group(['middleware' => 'auth'], function () {
Route::get('/', 'ProjectController@index');

Route::get('/projects/{id}', 'ProjectController@show');
Route::post('/create','ProjectController@store');
Route::get('/create', 'ProjectController@create');
Route::get('/projects/{id}/delete', 'ProjectController@destroy');
Route::put('/edit','ProjectController@update');
Route::get('/projects/{id}/edit', 'ProjectController@edit');

});

控制器:

 public function edit($id)
    {
        return view('project.edit',[

            'project' => Project::find($id)
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $project = Project::find($request->id);
        $project->project_name = $request->input('project_name');
        $project->client = $request->input('client');
        $project->description = $request->input('description');
        $project->time_span = $request->input('time_span');
        $project->text_report = $request->input('text_report');
        $project->created_by = $request->input('created_by');

        $project->save();

        return  redirect('/')->with('success', 'Project aangepast');
    }

您可以通过多种方式处理此问题:

There are multiple ways you can handle this:

  1. 如果坚持使用PUT,则可以将表单操作更改为POST,并添加具有值PUT的隐藏的method_field和隐藏的csrf字段(如果您使用的是Blade,您只需要添加@csrf_field{{ method_field('PUT') }}).这样,表单将接受请求.

  1. If you insist on using PUT you can change the form action to POST and add a hidden method_field that has a value PUTand a hidden csrf field (if you are using blade then you just need to add @csrf_field and {{ method_field('PUT') }}). This way the form would accept the request.

您可以简单地将route和form方法更改为POST.因为您是定义路线而不使用资源组的人,所以它会很好地工作.

You can simply change the route and form method to POST. It will work just fine since you are the one defining the route and not using the resource group.