PATCH Ajax request in Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Partial Updates: How to Execute AJAX PATCH Requests in Laravel

As developers building modern web applications with Laravel, we often deal with the need for efficient, granular updates to existing resources. When dealing with RESTful principles, knowing which HTTP verb to use is crucial. While POST is excellent for creating new resources and PUT (or PATCH) is designed for updating them, integrating these methods seamlessly with asynchronous JavaScript (AJAX) can sometimes feel like navigating a maze.

The core question we address here is: Is it possible to make AJAX PATCH requests to Laravel, or am I restricted to POST? The answer is a resounding yes. Laravel fully supports all standard HTTP verbs, and leveraging PATCH for partial updates is the most semantically correct approach for modifying only a portion of a record.

This guide will walk you through setting up the route, configuring the controller logic, and executing the request via JavaScript to achieve dynamic, real-time updates.

Understanding PATCH for Partial Updates

In RESTful API design, HTTP methods map directly to CRUD (Create, Read, Update, Delete) operations:

  • GET: Retrieve a resource.
  • POST: Create a new resource.
  • PUT / PATCH: Update an existing resource. PATCH is specifically designed for applying partial modifications to a resource, which is exactly what you need when using AJAX to update only specific fields without sending the entire resource payload.
  • DELETE: Remove a resource.

By using PATCH, you signal to the server that you intend to modify the resource identified by the URL, making your API design cleaner and more intuitive. As we explore this, remember that following these RESTful conventions is central to building robust applications on Laravel, much like adhering to the principles outlined by the framework itself.

Step 1: Defining the Route in Laravel

The first step is defining a route that explicitly listens for PATCH requests targeting a specific resource ID. This tells Laravel exactly which controller method should handle this type of request.

In your routes/web.php or routes/api.php file, you define the route as follows:

// routes/web.php

use App\Http\Controllers\QuestionController;
use Illuminate\Support\Facades\Route;

Route::patch('questions/{id}', [QuestionController::class, 'update'])->middleware('can:update_question');

By using Route::patch(), you are explicitly telling the framework that this endpoint is reserved for partial updates. The use of middleware, such as ->middleware('can:update_question'), ensures that only authorized users can trigger these updates, which is a critical security practice when dealing with data modification in Laravel applications.

Step 2: Implementing Logic in the Controller

The controller method must be prepared to receive the incoming data and perform the necessary database operations. Since this is an AJAX request, we focus on processing the incoming data efficiently.

// app/Http/Controllers/QuestionController.php

use Illuminate\Http\Request;
use App\Models\Question;

class QuestionController extends Controller
{
    public function update(Request $request, $id)
    {
        // 1. Authorization check (already handled by middleware, but good practice to re-check if needed)
        if (!auth()->user()->can('update_question')) {
            return response()->json(['error' => 'Unauthorized'], 403);
        }

        // 2. Validate the incoming data for safety
        $validatedData = $request->validate([
            'status' => 'required|string',
            // Add other fields you expect to update
        ]);

        // 3. Perform the partial update using Eloquent
        $question = Question::findOrFail($id);
        $question->update($validatedData);

        // 4. Return a JSON response for AJAX success
        return response()->json([
            'message' => 'Question updated successfully',
            'data' => $question
        ], 200);
    }
}

Notice how the controller focuses purely on processing the request data and returning a JSON response. This separation of concerns—routing handles where to go, the controller handles what to do—is the essence of good Laravel architecture.

Step 3: Executing the Request with JavaScript (AJAX)

Finally, we use JavaScript's fetch API or jQuery's $.ajax() to send the request asynchronously. This is where we specify the method as PATCH and include only the fields we intend to change in the payload.

// JavaScript Example using Fetch API (Modern approach)

document.getElementById('updateButton').addEventListener('click', function() {
    const questionId = document.getElementById('question_id').value;
    const newStatus = document.getElementById('status_input').value;

    fetch(`/questions/${questionId}`, {
        method: 'PATCH', // Specify the PATCH method
        headers: {
            'Content-Type': 'application/json',
            // Include CSRF token if using session-based authentication
        },
        body: JSON.stringify({ status: newStatus }) // Send only the required fields
    })
    .then(response => {
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
        return response.json();
    })
    .then(data => {
        console.log('Success:', data);
        // Update the UI based on the successful response
        document.getElementById('question_preview').innerHTML = `Status updated to: ${data.data.status}`;
    })
    .catch(error => {
        console.error('Error during PATCH request:', error);
    });
});

Conclusion

Making AJAX PATCH requests in a Laravel application is completely achievable and highly recommended for updating partial resource data. By correctly configuring the route with Route::patch(), writing clean, validated logic in your controller, and using modern JavaScript to construct the request payload, you establish a robust, efficient, and RESTful API interaction. This approach ensures that your Laravel backend remains scalable and adheres to best practices, making complex interactions feel intuitive for both developers and end-users.