【问题标题】:Laravel 6: The GET method is not supported for this route. Supported methods: POST ErrorLaravel 6:此路由不支持 GET 方法。支持的方法:POST 错误
【发布时间】:2020-05-15 15:00:29
【问题描述】:

我是 Laravel 6 的新手,我正在尝试创建一个编辑配置文件功能,但我遇到了错误:

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

老实说,我不确定为什么会出现此错误。我已经交叉检查了所有内容。

配置文件控制器 更新函数

public function update(Request $request, $id)
    {
        $profile->nickname = $request->input('nickname');
        $profile->name = $request->input('name');
        $profile->birthday = $request->input('birthday');
        $profile->save(); //persist the data
        return redirect()->route('profile.index')->with('info','Profile got saved');
    }

我的路线文件:

Route::get('/profile', 'ProfileController@index')->name('profile');

Route::put('/profile/edit/{profile}','ProfileController@update')->name('profile.update');

edit.blade.php

<form action="{{route('profile.update')}}" method="POST">
                        @csrf
                        @method('PUT')

                        <div class="form-group row">
                            <label for="nickname" class="col-md-4 col-form-label text-md-right">{{ __('Brugernavn') }}</label>

                            <div class="col-md-6">
                                <input id="nickname" type="text" class="form-control @error('nickname') is-invalid @enderror" name="nickname" value="{{ Auth::user()->nickname }}">
                            </div>
                        </div>

                        <!-- Submit -->
                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-secondary">
                                    Gem
                                </button>
                            </div>
                        </div>
                    </form>

【问题讨论】:

  • 你添加了欺骗表单方法,但是 Laravel 没有运行 put 动作。你应该执行php artisan route:list 来检查第一个匹配的规则。
  • 您的路线不正确,您的路线定位错误。检查您的路线,要进行编辑,您必须传递一个 id,但对于更新,您还必须传递数据和 id,如果您使用过 PUT,那么您的路线方法也将是 PUT,如果它是 @987654329 @方法在你的路由中,那么你不需要在你的表单中使用PUT方法。
  • 只是出于好奇:Route::post 不是很清楚吗? ;)

标签: php laravel-6


【解决方案1】:

通常 Laravel 提供了 5 种方法。

GET/contacts, mapped to the index() method and shows contacts list,
GET /contacts/create, mapped to the create() method and shows create form,
POST /contacts, mapped to the store() method and handle create form request,
GET /contacts/{contact}, mapped to the show() method and shows single item,
GET /contacts/{contact}/edit, mapped to the edit() method and shows update form,
PUT/PATCH /contacts/{contact}, mapped to the update() method and handle update form request,
DELETE /contacts/{contact}, mapped to the destroy() method and handle delete form request.

你必须改变你的 route.php 文件

Route::put('/profile/edit/{profile}','ProfileController@update')->name('profile.update');

在你的表单中,改变动作

<form action="{{ route('profile.update', Auth::user()->id) }}" method="POST">
...
</form>

更多:https://www.techiediaries.com/php-laravel-crud-mysql-tutorial/

【讨论】:

  • 更改为您建议的内容后,我得到了 404,所以我无法以某种方式找到它。编辑和更新是否相互关联?
  • 感谢您将我链接到 crud 教程。我让它工作了。我唯一关心的是编辑时的网址?输入edit.blade时,url是:/profile/16/edit等,但是使用laravel 6时可以改变吗?编辑配置文件仅供用户本人使用,不包括管理员或其他用户
  • 就像我登录另一个用途并输入 url:/profile/16/edit,它显示用户 id:17 信息,但是当尝试编辑时,它试图去 id 17,是有办法解决吗?
【解决方案2】:
You are getting the error because your route expects post and you are using get method

Route::get('/profile/edit','ProfileController@edit')->name('profile.edit');


Route::put('/profile/','ProfileController@update')->name('profile.update');


public function edit() {

return view (edit);
}

public function update() {

//things to update
}

【讨论】:

    【解决方案3】:

    在你的edit.blade.php文件中删除

     @method('PUT')
    

    那么您的方法将只发布

    【讨论】:

      【解决方案4】:

      你可以任意设置路由方法

      Change Route::post('/profile/edit','ProfileController@update')->name('profile.update'); 
      to 
      Route::any('/profile/edit','ProfileController@update')->name('profile.update'); change 
      

      谢谢

      【讨论】:

        猜你喜欢
        • 2020-04-25
        • 2020-01-23
        • 1970-01-01
        • 2021-04-11
        • 1970-01-01
        • 2021-08-05
        • 2020-06-05
        • 2020-03-21
        • 2021-01-10
        相关资源
        最近更新 更多