Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CarbonNanotubes's avatar

405 Method Not Allowed

I am building an new web app with Laravel 6.3. I am testing the small API I have built. I tested a get all, get one, and create requests but I have run into an issue with the put/patch request.

Here are my routes in api.php

Route::prefix('room')->name('room.')->group(function () {
    Route::get('/', 'RoomController@index')->name('index');
    Route::get('/{room}', 'RoomController@show')->name('show');
    Route::post('/', 'RoomController@store')->name('store');
    Route::put('/{room}', 'RoomController@update')->name('update');
    Route::delete('/{room}', 'RoomController@delete')->name('delete');
});

when I make a put request to example.com/api/room/3 and i get a 405 Method Not Allowed: The PUT method is not supported for this route. Supported methods: GET, HEAD.

If I change the request to a patch request I get the same thing. If I set the request to patch in api.php and send a put request I get 405 Method Not Allowed: The PUT method is not supported for this route. Supported methods: GET, HEAD, PATCH, DELETE. Which I expect. I change the request to a patch request and i get a 405 Method Not Allowed: The PUT method is not supported for this route. Supported methods: GET, HEAD.

Whats going on here?

0 likes
11 replies
CarbonNanotubes's avatar

The methods were generated by php artisan make:model Room -mr

<?php

namespace App\Http\Controllers;

use App\Room;
use Illuminate\Http\Request;

class RoomController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return response()->json([ 'data' => Room::all()]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|unique:rooms|min:3'
        ]);

        $room = Room::create(['name' => $request->name]);

        return response()->json([ 'data' => $room ], 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Room  $room
     * @return \Illuminate\Http\Response
     */
    public function show(Room $room)
    {
        return response()->json([ 'data' => $room ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Room  $room
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Room $room)
    {
        $request->validate([
            'name' => 'required|min:3'
        ]);

        $updatedRoom = $room->update(['name' => $request->name]);

        return response()->json([ 'data' => $updatedRoom ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Room  $room
     * @return \Illuminate\Http\Response
     */
    public function destroy(Room $room)
    {
        return response()->json([ $room->delete() ]);

    }
}

siangboon's avatar

the controller is not necessary at this point as it not even pass through the route yet.... I meant your blade code?

CarbonNanotubes's avatar

If I comment out all the code from the update method, it works fine. I uncomment the validation code and I get the error again

CarbonNanotubes's avatar
Level 19

I found the issue. I send my data with a multipart form body. This was working for post but it seems the data was not being sent causing an issue with validation which then causes the routing issue.

I switched it to JSON body and now it works. My guess is there is an issue when validation fails for an item that was not sent with the payload.

JosephNC's avatar

I know this is coming late but it may help somebody.

For some reason with laravel 8, validating with the $request object throws the MethodNotFoundException

$request->validate([
    'title' => 'required|string'
]);

So I had to use the Validator Class from Illuminate\Support\Facades\

$validator = Validator::make($request->all(), [
    'title' => 'required|string'
]);

That fixed it for me.

1 like
halvarado-7s's avatar

Gracias @josephnc , no era exactamente la respuesta, pero me hizo mirar hacia la validación del request, donde tenia un error

Please or to participate in this conversation.