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
Clicking a
<a href
tag always causes the browser to generate a HTTP GET request. But withRoute::post
you defined your route as only accepting HTTP POST requests.To resolve it you can either:
And as a background task, make sure you understand what HTTP methods are. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
Use this instead
Route::get
and<a href="{{route('post_wishlist')}}">
If you want to store data use Post ex.