我试图从列表中删除购物车..当我试图删除它时,它显示上面的错误。
这是我的控制器,查看部件和方法
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.
更多推荐
发布评论