【问题标题】:The GET method is not supported for this route. Supported methods: PATCH此路由不支持 GET 方法。支持的方法:PATCH
【发布时间】:2020-04-23 06:39:14
【问题描述】:

请问我找不到问题出在哪里!

刀片

<form action="/profile/{{$user->id}}" enctype="multipart/form-data" method="post">
                @csrf
                @method('patch')

路线

Route::patch('/profile/{user}', 'ProfilesControler@update')->name('profile.update');

错误:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 此路由不支持 GET 方法。支持的方法: 补丁。

编辑 1- web.php

Route::get('/', function () { return view('welcome'); }); 
Auth::routes(); 
Route::get('/p/create','postsController@create'); 
Route::get('/p/{posts}','postsController@show'); 
Route::post('/p','postsController@store'); 
Route::get('/profile/{user}/edit', 'ProfilesControler@edit')->name('Profile.edit'); 
Route::get('/Profile/{user}', 'ProfilesControler@index')->name('Profile.show');
Route::post('/profile/{user}', 'ProfilesControler@update')->name('profile.update')

【问题讨论】:

  • 确保您提交的是该表单,而不是另一个包裹该表单的表单
  • 嗨 Alberto,我的 edit.blade.php 上只有一个表单,我在 creat.blade.php 上有另一个表单,但它使用的是另一条路径,我疯了,我丢失了一个 dat试图修复它!希望你能帮助我
  • 你会在这里分享整个web.php文件吗!
  • 一定要加载这个视图,而不是另一个视图...还要检查浏览器中的源文件,以使用 @method 指令检查 laravel 在页面中的回显

标签: php laravel post patch


【解决方案1】:

试试这个:

<form action="/profile/{{$user->id}}" enctype="multipart/form-data" method="post">
                @method('PATCH')
                @csrf

</form>

另一个提示总是使用route method 作为路由,因为您在web.php 中命名了路由,因此它易于维护和调试

<form action="{{route('profile.update',['user' => $user->id])}}" enctype="multipart/form-data" method="post">
                @method('PATCH')
                @csrf
</form>

阅读更多关于method spoofing here

希望对你有帮助..

谢谢。

注意:如果问题仍然存在,请尝试清除您的路由缓存php artisan route:clear

【讨论】:

  • 我改了,但问题依旧,这个错误让我抓狂!
  • 你能发布你的整个 web..php 文件吗?
  • 这里是:Route::get('/', function () { return view('welcome'); });授权::路由(); route::get('/p/create','postsController@create'); route::get('/p/{posts}','postsController@show'); route::post('/p','postsController@store'); Route::get('/profile/{user}/edit', 'ProfilesControler@edit')->name('Profile.edit'); Route::get('/Profile/{user}', 'ProfilesControler@index')->name('Profile.show'); Route::post('/profile/{user}', 'ProfilesControler@update')->name('profile.update')
  • @MouradMohammedBenosman 将此 Route::post('/profile/{user}', 'ProfilesControler@update')-&gt;name('profile.update') 更改为 Route::patch('/profile/{user}/update', 'ProfilesControler@update')-&gt;name('profile.update')
  • 当我删除该行时 return redirect("/profile/1");来自:公共函数更新(用户 $user){ $data=request()->validate([ 'title'=>'required', 'description'=>'required', 'Url'=>'url', '图像'=>'', ]); $user->profile->update($data);返回重定向(“/profile/1”); } } 有效,不明白为什么!?
【解决方案2】:

更改&lt;form action="/profile/{{$user-&gt;id}}" enctype="multipart/form-data" method="post"&gt;

收件人:&lt;form action="/profile/{{$user-&gt;id}}" enctype="multipart/form-data" method="GET"&gt;

再试一次

注意:您不能使用 GET 上传图片,

编辑:给你的路线起个名字然后使用它

like : Route::post('/update', 'ProfilesControler@update'); 或者您可以切换补丁以发布或获取

或:Route::patch('/update', 'ProfilesControler@update')-&gt;name('update');

Route::patch('/update', 'ProfilesControler@update')->name('update');

【讨论】:

  • 同样的错误 Dilek!此路由不支持 GET 方法。支持的方法:PATCH。
  • 我使用了 get,它可以工作,但我认为当我想更新表格时最好使用 patch 或 put 对吗?
  • @MouradMohammedBenosman 我更新了答案尝试命名您的路线,如我在答案中显示的那样
  • 我更改了所有 web.php :此路由不支持 PATCH 方法。支持的方法:POST。
  • 查看@SalmanZafar 编辑的您自己的问题,您需要在我的问题中更改补丁以发布方法。在此处查看更多信息github.com/tymondesigns/jwt-auth/issues/1843
【解决方案3】:

在回答您的问题之前。让我们阅读一些事实,要执行resource 方法,您必须遵循一些最佳实践。

Uri 必须遵循这种模式 /controllerName/methodName/{unique_identifier}

对于@index,@store

Route::get('/profile/','ProfileController@index');
Route::post('/profile/','ProfileController@store');

以上两条路线相同,但方法不同。

对于@show

Route::get('/profile/{user_id}','ProfileController@show');

对于@create、@edit

Route::get('/profile/create','ProfileController@create');
Route::get('/profile/edit/{user_id}','ProfileController@edit');

对于@update,@destroy

Route::put('/profile/update/{user_id}','ProfileController@update');
Route::delete('/profile/destroy/{user_id}','ProfileController@destroy');

或者你可以使用

Route::post('/profile/update/{user_id}','ProfileController@update');
Route::post('/profile/destroy/{user_id}','ProfileController@destroy');

对于更新请求,您的method 应为PUT;所以

<form method="POST">
  @csrf
  @method('PUT')
  <!---whatever your code -->
</form>

或

{{ Form::open(array('url' => '/', 'method' => 'PUT')) }}
    .... wathever code here
{{ Form::close() }}

注意:由于 HTML 表单仅支持 POST 和 GET、PUT 和 DELETE 方法将通过自动添加 _method 隐藏字段来进行欺骗 到你的表格。

在您的情况下, method 未设置,因为它的路由接受 PATCH 方法,所以请检查您的 @method('PATCH') 指令在您的 input element 中回显关联 input element,如预期的那样关注

<input name="_method" type="hidden" value="PATCH">

您必须将 ProfilesControler 更改为 ProfileController,因为它是 拼写错误

【讨论】:

    【解决方案4】:

    Gys,我确定是因为我没有发送正确的数据,它不能正常工作,这是我的更新功能:公共功能

    更新(用户 $user){

    $data=request()->validate([
    
        'title'=>'required',
        'description'=>'required',
        'Url'=>'url',
    
    
    ]);
    
    
    $user->profile->update($data);
    return redirect("/profile/{$user->id}");
    

    } }

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 2020-12-06
      • 1970-01-01
      相关资源
      最近更新 更多