The Route Verb Mismatch in Laravel Delete Operations
When implementing a delete feature in a Laravel application, it is common to encounter a scenario where the user clicks a link expecting a deletion, but instead sees the resource displayed again. This specific behavior often stems from a mismatch between the HTTP verb used by the browser and the route definition.
In your example, the issue lies entirely within how you are invoking the destroy action versus how it is defined in your routing group. You have configured the controller method $blogController@destroy to handle a DELETE request:
Route::delete('/nieuws/{id}', 'blogController@destroy')->name('nieuws.destroy');
However, your view renders a standard HTML anchor tag using the route() helper with a hardcoded URL structure that implicitly triggers a GET request. The link looks like this:
<a href="{{route('nieuws.destroy', ['id' => $blog->id])}}" onclick="return confirm('Weet je dit zeker?')">
<i class="fa fa-trash"></i>
</a>
When you execute {{ route('nieuws.destroy', [...]) }}, Laravel generates a URL such as /nieuws/5. Because the anchor tag defaults to an HTTP GET request, your routing group matches this against the existing Route::get('/nieuws/{blog}', ...) definition. Since {blog} is a model binding pattern that accepts any string, it happily captures the ID 5, binds it to the Blog model, and renders the show view instead of executing the destruction logic.
The Correct Approach for State-Changing Actions
HTTP verbs are designed with specific purposes. GET requests should be safe and idempotent; they retrieve data without side effects. Conversely, DELETE requests change state by removing resources. When a user clicks a button to delete data, the browser must send a DELETE verb.
To fix this, you have two primary options depending on your desired user experience and security posture.
Option 1: Use an HTTP Method Field (Recommended for Forms)
If you are building a robust application, the standard pattern is to use a form with a hidden input specifying the method, even if the action is rendered as a button. This ensures the request arrives as DELETE regardless of browser behavior or caching issues.
<form action="{{ route('nieuws.destroy', ['id' => $blog->id]) }}" method="POST">
{{ csrf_field() }}
<button type="submit" onclick="return confirm('Weet je dit zeker?')">
<i class="fa fa-trash"></i> Delete
</button>
</form>
By wrapping the delete action in a <form> tag and setting method="POST", you can utilize Laravel's "HTTP method field" feature. Inside the form, a hidden input named _method is added with the value DELETE. The router will then strip this field before matching against your route definitions, effectively treating the request as a DELETE call while maintaining browser compatibility (browsers often block direct DELETE or PUT/PATCH verbs on links due to history state concerns).
Option 2: Use JavaScript to Trigger the Delete Verb
If you prefer to keep the implementation clean without forms, you must explicitly trigger the correct HTTP verb using JavaScript. This approach prevents the browser from following a redirect to a GET endpoint automatically.
@csrf
<form id="delete-form" method="POST" style="display: none;">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
<a href="#" onclick="
event.preventDefault();
document.getElementById('delete-form').submit();
return false;
">
<i class="fa fa-trash"></i>
</a>
Security Considerations
It is critical to include {{ csrf_field() }} in any form that modifies state, including delete operations. Your current controller logic does not appear to check for this token, which leaves your application vulnerable to Cross-Site Request Forgery (CSRF) attacks. An attacker could trick a logged-in user into visiting a malicious link that deletes their own data or deletes data they shouldn't be able to access.
Additionally, ensure that your destroy method validates the ID before attempting deletion. While your current code uses model binding logic implicitly via $blog = Blog::find($id), relying solely on find can lead to 404 errors if the resource doesn't exist. A more robust implementation might use conditional chaining or explicit existence checks:
public function destroy($id) {
$blog = Blog::findOrFail($id); // Throws a helpful 404 if not found
$blog->delete();
return redirect()->route('blogs.index')->with('success', 'Blog deleted successfully.');
}
By aligning your route definitions with the actual HTTP verbs sent by your views, you ensure that state-changing operations behave predictably and securely within the Laravel framework.