简体   繁体   English

PUT请求Laravel上的MethodNotAllowedException

[英]MethodNotAllowedException on PUT request Laravel

I'm trying to do a PUT request on my form using Laravel, and it is returning a MethodNotAllowedException, the form is as follows: 我正在尝试使用Laravel在表单上执行PUT请求,并且返回MethodNotAllowedException,该表单如下所示:

<form role="form" method="POST" action="{{ route('negocio.update', $negocio->id) }}" enctype="multipart/form-data">

{{ method_field('PUT') }}

</form>

And my route is registered as: 我的路线注册为:

Route::put('/update/{id}', ['as' => 'negocio.update', 'uses' => 'client\\NegocioController@update']);

Using the php artisan route:list command gives me the following route: 使用php artisan route:list命令可以给我以下路由:

| PUT | update/{id} | negocio.update | App\\Http\\Controllers\\client\\NegocioController@update | web

Can someone explain me, what am I doing wrong? 有人可以解释我,我在做什么错吗? I have already searched on different sites but no solution. 我已经在其他站点上搜索了,但没有解决方案。

*UPDATE: *更新:

I opened the browser inspector to see what kind of request was doing and it is trying to access another route using get method, which I do not have registered so that's why the error is displaying, but the thing is, I don't know why the form is submitting to another url. 我打开了浏览器检查器,以查看正在执行哪种请求,并且它正在尝试使用get方法访问另一条路由,而该方法尚未注册,因此这就是为什么显示错误,但事实是,我不知道为什么表单正在提交到另一个URL。

If the negocio is a resource controller, and you have it in the web.php file like this: Route::resource('negocio','NegocioController'); 如果negocio是资源控制器,并且您将其放在web.php文件中,如下所示: Route::resource('negocio','NegocioController'); , try and do in like this: ,尝试这样做:

    <form method="POST" action="{{route('negocio.update', $negocio->id)}}" enctype="multipart/form-data">
    {{ method_field('PUT') }}{{csrf_field()}}
    </form>

But dont use this(delete it or comment it out): 但不要使用此(删除或注释掉):

Route::put('/update/{id}', ['as' => 'negocio.update', 'uses' => 'client\NegocioController@update']);

If it's not a resource controller , then declare it in the route file like so: 如果不是资源控制器 ,则在路由文件中声明它,如下所示:

Route::put('/update/{id}', 'NegocioController@update')->name('negocio.update');

If this dont do the trick, then try it with Post, and let the {{method_field('Put') do the trick. 如果这样做{{method_field('Put') ,请与Post一起尝试,然后让{{method_field('Put')

Route::post('/update/{id}', 'NegocioController@update')->name('negocio.update');

Hope this helps. 希望这可以帮助。 and if so, please tell us here. 如果是这样,请在这里告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM