skip to Main Content

I am adding a route in my Laravel application for an "add to wishlist" feature, but I am getting "method not supported" when I try to use it.

I have added an anchor tag and defined its route in the href:

<a href="{{url('/post_wishlist')}}" id="addtowishlist"><i class="fa fa-heart fa-2x addtowishliststyle" aria-hidden="true" style="color:' + (cardimg['inWishlist'] ? '#8B0000' : 'white') + '"></i></a>

I have also created a route in the web.php file:

Route::post('/post_wishlist', [MyCartController::class, 'post_wishlist'])->name('post_wishlist');

public function post_wishlist()
{
    echo "add to wishlist called";
}

When clicking the link I get the following error:

The GET method is not supported for route post_wishlist. Supported methods: POST

Is there an issue with the anchor tag?

2

Answers


  1. Clicking a <a href tag always causes the browser to generate a HTTP GET request. But with Route::post you defined your route as only accepting HTTP POST requests.

    To resolve it you can either:

    • redefine the route to accept GET, or
    • replace the anchor tag with a form and a button which can generate a POST request.

    And as a background task, make sure you understand what HTTP methods are. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

    Login or Signup to reply.
  2. Use this instead Route::get and <a href="{{route('post_wishlist')}}">

    If you want to store data use Post ex.

        Route::post('/post_wishlist', [MyCartController::class, 'post_wishlist'])->name('post_wishlist');
        
        
        <form action="{{route('post_wishlist')}}" method="POST">
         @csrf
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search