0
0
LaravelHow-ToBeginner · 4 min read

How to Use Route::put in Laravel for HTTP PUT Requests

In Laravel, use Route::put to define a route that handles HTTP PUT requests, typically for updating data. It takes a URL path and a controller action or closure to process the request. This method ensures your app responds correctly to PUT requests from forms or APIs.
📐

Syntax

The Route::put method defines a route that listens for HTTP PUT requests. It requires two main parts:

  • URL path: The endpoint where the request is sent.
  • Action: A controller method or closure that handles the request.

This route is used to update existing resources.

php
Route::put('/resource/{id}', [ResourceController::class, 'update']);
💻

Example

This example shows how to define a PUT route to update a user's information using a controller method. The route listens at /user/{id} and calls the update method in UserController.

php
<?php

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

Route::put('/user/{id}', [UserController::class, 'update']);

// UserController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function update(Request $request, $id)
    {
        // Simulate updating user data
        $name = $request->input('name');
        // Normally, update user in database here
        return response()->json([
            'message' => "User {$id} updated successfully.",
            'new_name' => $name
        ]);
    }
}
Output
{"message":"User 5 updated successfully.","new_name":"Alice"}
⚠️

Common Pitfalls

Common mistakes when using Route::put include:

  • Not sending the request as a PUT method (e.g., using GET or POST instead).
  • Missing the @method('PUT') directive in HTML forms, which is required because browsers only support GET and POST natively.
  • Forgetting to include the resource ID in the URL if needed.
  • Not handling validation or missing request data in the controller.

Always ensure your form or client sends a PUT request and your route matches the URL pattern.

php
<?php
// Wrong: Using POST route instead of PUT
Route::post('/user/{id}', [UserController::class, 'update']);

// Right: Use PUT route
Route::put('/user/{id}', [UserController::class, 'update']);

// In Blade form, include method spoofing:
// <form method="POST" action="/user/5">
//     @csrf
//     @method('PUT')
//     <!-- form fields -->
// </form>
📊

Quick Reference

ConceptDescription
Route::putDefines a route for HTTP PUT requests, used for updating resources.
URL PathThe endpoint URL, often includes resource ID (e.g., /user/{id}).
ActionController method or closure that processes the PUT request.
Form Method SpoofingUse @method('PUT') in HTML forms to send PUT requests.
ResponseTypically returns JSON or redirects after update.

Key Takeaways

Use Route::put to handle HTTP PUT requests for updating resources in Laravel.
Include the resource identifier in the route URL to specify which item to update.
In HTML forms, use @method('PUT') to spoof the PUT method since browsers only support GET and POST.
Always validate and handle input data in the controller method that processes the PUT request.
Route::put routes should match the HTTP method sent by the client to work correctly.