PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 20, 2022

[FIXED] Laravel Delete Method: MethodNotAllowedHttpException in RouteCollection.php line 233:

 February 20, 2022     laravel, laravel-5, php     No comments   

Issue

I tried to delete the cart from a list. When I tried to delete it, it shows an Error. Below here is my code:

Web.php

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

blade.php

<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>

Controller.php

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

Solution

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.



Answered By - Yasas Gunarathne
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing