Method not allowed when PUT used over AJAX for Laravel resource
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Resolving the "Method Not Allowed" Error: Using PUT with AJAX in Laravel Resources
As developers working with modern web applications, integrating front-end interactivity via AJAX into a robust back-end framework like Laravel is a daily occurrence. However, navigating the nuances of HTTP methods—especially when dealing with RESTful resource updates—can often lead to frustrating errors like the dreaded 405 Method Not Allowed.
This post dives deep into the specific issue you are facing: attempting to use the PUT method via AJAX to update a Laravel resource defined by Route::resource, and why this sometimes fails while standard form submissions succeed. We will diagnose the problem and provide the correct, robust solution.
The Mystery of the 405 Error in RESTful Updates
You have correctly identified the conflict: your route is set up for resource management, and you are trying to perform an update operation using PUT over AJAX.
The core reason this happens lies in how Laravel's routing system expects methods to be handled, particularly when dealing with standard form submissions versus raw API calls.
When you use a traditional HTML form submission (even with the older _method=PUT trick), the server-side framework handles the request flow, often managing the method redirection internally. However, when you execute a direct, raw AJAX call ($.ajax({ method: 'PUT', ... })), Laravel's built-in middleware and routing checks are stricter. They enforce that methods like PUT or PATCH must be explicitly mapped to controller methods that are expecting them, often leading to the 405 Method Not Allowed error if the route definition isn't fully accommodating the request type in that specific context.
Understanding Laravel Routing and Eloquent Updates
In a standard Laravel setup utilizing Route::resource, you have defined several routes: index, create, store, show, edit, and update. The update route is typically configured to accept PUT or PATCH requests.
The problem often isn't with your JavaScript code, but how the initial request is being interpreted by Laravel before it even reaches your controller logic. While Eloquent models are designed to handle updates seamlessly, the HTTP layer requires proper routing configuration. Understanding this structure is key when building APIs on top of Laravel (referencing best practices found at https://laravelcompany.com).
The Solution: Ensuring Correct Route Mapping and Data Handling
To successfully perform an update via AJAX, you need to ensure two things are correctly aligned: the route definition and the data payload structure.
1. Verify Your Route Definition
Ensure your resource routes are defined correctly. For standard RESTful updates, PUT or PATCH should map directly to the update method in your controller.
// routes/web.php (or api.php)
Route::resource('items', 'ItemsController', ['before' => 'admin_access']);
This setup is correct for a standard resource. The failure usually stems from how the data is being sent or how Laravel interprets the request context during an AJAX call.
2. Correcting the AJAX Payload and Method Request
When performing an update via AJAX, you must ensure the payload structure mimics what your controller expects, and that the method verb is correctly associated with the URL.
Your JavaScript logic for serializing data and including the _method parameter is fundamentally correct for simulating a form submission:
var id = $(row).find('.edit').data('id');
var data = $inputs.serializeJSON();
data['_token'] = $('input[name=_token]').val();
data['_method'] = 'PUT'; // This tells Laravel to treat the incoming request as a PUT operation
$.ajax({
url: 'items/' + id,
method: 'PUT', // The AJAX method must match the intended REST verb
dataType: 'json',
data: data,
// ... rest of the callback
});
If you are still encountering the 405 error with this structure, it often signals one of two deeper issues:
- Middleware Conflict: A custom middleware or route group is intercepting the request and blocking the
PUTverb before it reaches the controller. Review any custom route definitions or global middleware applied to these routes. - Route Versioning/API Structure: If you are building a pure API (not using web routes), ensure your API structure aligns with Laravel's expectations for resource manipulation, as detailed in official documentation regarding API design (https://laravelcompany.com).
Conclusion: The Developer Takeaway
The difference between a successful form submission and a failing AJAX request often boils down to the explicit handling of HTTP verbs on the server side versus the client side. While using _method=PUT is a clever trick for traditional HTML forms, when moving to modern JavaScript-driven APIs, the most reliable method is to ensure your front-end request's method property exactly matches the expected route definition (PUT or PATCH) and that no intermediate middleware is accidentally blocking the update operation.
By carefully inspecting your routes, middleware stack, and the exact payload structure sent via AJAX, you can resolve this common RESTful headache and ensure smooth data manipulation in your Laravel applications.