use DELETE method in route with Laravel 5.4

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Using DELETE Method in Routes with Laravel 5.4: A Comprehensive Guide Body:

Laravel provides an elegant way of handling different HTTP requests through its routing system and various methods such as GET, POST, PUT, and DELETE. In this blog post, we will focus on using the DELETE method in a Laravel 5.4 project to handle deletions for categories in your application. We'll cover the necessary steps, possible issues, and best practices while handling such requests.

Setting Up the View

First, let's create the view using Blade template engine to display a list of categories and their respective delete buttons:
<table class="table">
  <thead>
    <th>Name</th>
    <th>Action</th>
  </thead>
  <tbody>
    @foreach ($categories as $category)
      <tr>
        <td>$category->name</td>
        <td>
          <a href="{{ url('/categories', ['id' => $category->id]) }}">
            <button class="btn btn-default">
            Delete
            </button>
          </a>
        </td>
      </tr>
    @endforeach
  </tbody>
</table>
This code will generate a table with the category names and a delete button for each row. Clicking on the "Delete" button will send an HTTP DELETE request to the specified route.

Routing and the Route Method

Next, we'll create the appropriate route in our web.php file:
Route::delete('/categories/{id}', CategoryController@destroy);
This line of code will map all DELETE requests directed towards "/categories/[id]" to the "destroy()" method in our CategoryController class. We can create this controller with a simple function that handles this request:
namespace App\Http\Controllers;

use Illuminate\Routing\Route;
use DB;

class CategoryController extends Controller {
    public function destroy($id) {
        // Perform logic to delete the category with the specified id.
        $category = DB::table('categories')->where('id', $id)->first();
        if (isset($category)) {
            $category->delete();
        }

        return redirect()->route('list-categories');
    }
}
This controller will first retrieve the category with the specified ID and then delete it. Finally, it will redirect back to the list of categories.

Addressing Potential Issues

If you've followed these steps but still encounter an error such as "Method [DELETE] is not supported by URL", it means your browser might be sending an incorrect request type. In this case, we can try forcing the request to use the DELETE method:
<!DOCTYPE html>
<html lang="en">
  <head>
    ...
  </head>
  <body>
    
    ...
  </body>
</html>
In this example, we select the form element and set its "method" attribute to "DELETE". This should force the browser to send a DELETE request when submitting the form. Alternatively, you could try using JavaScript to create a custom AJAX call which explicitly sets the HTTP method as DELETE. In conclusion, this comprehensive guide highlights the necessary steps and best practices for handling DELETE requests through Laravel 5.4 routes and controllers. By following these guidelines, your application can effectively manage deletion operations while maintaining efficient routing performance. Remember to always keep your code clean and well-structured for future maintenance and updates.