Laravel Delete方法:RouteCollection.php第233行中的MethodNotAllowedHttpException:(Laravel Delete Method: Met

编程入门 行业动态 更新时间:2024-10-23 09:37:19
Laravel Delete方法:RouteCollection.php第233行中的MethodNotAllowedHttpException:(Laravel Delete Method: MethodNotAllowedHttpException in RouteCollection.php line 233:)

我试图从列表中删除购物车..当我试图删除它时,它显示上面的错误。

这是我的控制器,查看部件和方法

Route::post('cart/delete/{id}','ProductController@deleteCart'); <a href="{{ url('/cart/delete',$row->id) }}" class="remove_item"> <i class="fa fa-times"></i> </a> <form action="{{ url('/cart/delete',$row->id)}}" method="POST" style="display: none;"> {!! Form::hidden('id',$row->id) !!} </form> public function deleteCart($id){ $cart = Cart::find($id); $cart->destroy(); return Redirect::to('/shop-cart'); }

I tried to delete cart from a list..when i tried to delete it, it shows the Above Error.

Here is my controller, view part and method

Route::post('cart/delete/{id}','ProductController@deleteCart'); <a href="{{ url('/cart/delete',$row->id) }}" class="remove_item"> <i class="fa fa-times"></i> </a> <form action="{{ url('/cart/delete',$row->id)}}" method="POST" style="display: none;"> {!! Form::hidden('id',$row->id) !!} </form> public function deleteCart($id){ $cart = Cart::find($id); $cart->destroy(); return Redirect::to('/shop-cart'); }

最满意答案

只需更改以下代码行:

Route::post('cart/delete/{id}','ProductController@deleteCart');

成:

Route::get('cart/delete/{id}','ProductController@deleteCart');

此错误的原因是向POST路由发送GET请求。 在您的代码中,您通过调用URL发送GET请求。

<a href="{{ url('/cart/delete',$row->id) }}" class="remove_item"> <i class="fa fa-times"></i> </a>

或者,如果您想保持路线不变(作为POST路线),只需使用以下代码并相应地进行一些调整:

<form action="{{ url('/cart/delete') }}" method="POST" style="display: none;"> {!! Form::hidden('id', $row->id) !!} <input type="submit" value="Submit"> </form>

最好修改路由如下,因为我们正在发送id和POST请求,因此不需要'/{id}'部分:

Route::post('cart/delete','ProductController@deleteCart');

使用以下方法将Http \ Request导入控制器:

use Illuminate\Http\Request;

并按如下方式更新控制器功能:

public function deleteCart(Request $request){ $cart = Cart::find($request['id']); $cart->destroy(); return Redirect::to('/shop-cart'); }

但对于这种情况,GET路线似乎是避免复杂性的好选择。

Simply change the following line of code:

Route::post('cart/delete/{id}','ProductController@deleteCart');

into:

Route::get('cart/delete/{id}','ProductController@deleteCart');

Reason for this error is sending a GET request to a POST route. In your code you are sending a GET request by calling a URL.

<a href="{{ url('/cart/delete',$row->id) }}" class="remove_item"> <i class="fa fa-times"></i> </a>

Or otherwise if you want to keep the route as it is (as a POST route) just use the following code and make some adjustments accordingly:

<form action="{{ url('/cart/delete') }}" method="POST" style="display: none;"> {!! Form::hidden('id', $row->id) !!} <input type="submit" value="Submit"> </form>

And it is better to modify the route as follows as the '/{id}' part is not needed as we are sending the id along with the POST request:

Route::post('cart/delete','ProductController@deleteCart');

Import Http\Request into your controller using:

use Illuminate\Http\Request;

And update your controller function as follows:

public function deleteCart(Request $request){ $cart = Cart::find($request['id']); $cart->destroy(); return Redirect::to('/shop-cart'); }

But for this scenario GET route seems a good choice to avoid complexity.

更多推荐

本文发布于:2023-08-04 02:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   RouteCollection   Laravel   Delete   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!