I have a button on my CRUD application that looks like this:
<a class="btn btn-danger" onclick="return confirm('Are you sure?')" href="{{route('topic.destroy', $topic->id)}}">
When the user clicks the button it loads the show
method in my resource controller and not the destroy
method.
My routes:
Route::resource('/main_topic', 'MainTopicController'); Route::resource('/topic', 'TopicController'); Route::resource('/post', 'PostController');
Why is it not using the correct controller method?
Advertisement
Answer
Any HTTP action pointing to POST, PUT, or DELETE routes that are defined in the web routes file should include a CSRF token field. Otherwise, the request will be rejected. You can read more about CSRF protection in the CSRF documentation:
<form method="POST" action="{{route('topic.destroy', $topic->id)}}"> @csrf @method('Delete') <button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure?')">Delete</button> </form>