Search code examples
phplaravellaravel-routing

Why does Laravel say that my "update" method is not a support route?


I am getting this error when I try to use update method from my routes.

The UPDATE method is not supported for this route. Supported methods: GET, HEAD, POST, DELETE.

I am building my project using Laravel and have defined update by route in web.php

Route::get('/{post:slug}', [PostController::class, 'show']);
Route::delete('/{post:slug}', [PostController::class, 'destroy']);
Route::post('/{post:slug}', [PostController::class, 'create']);
Route::post('/{post:slug}', [PostController::class, 'update']);

And my update function in PostController.php should just ddd.

public function UPDATE(Post $post)
    {
        ddd(request(),$post);
    }

and as far as I have seen so long as I have @csrf and method("UPDATE") everything should be good. This is on my posts.show.blade.php

<form method="POST" action="/{{$post->slug}}">
    @csrf
    @method("UPDATE") 

I'm new to Laravel and MVC; where have I gone wrong or what am I missing? On the page where this is failing I have two modal forms, one to update/edit and one to delete. The delete method works just fine but update fails with the same setup.

To also be clear, these forms are on /{{$post->slug}} could the issue be that I am trying to route to /{{$post->slug}}/{{$post->slug}} with the form action so that then throws off the route?


Solution

  • As a user already stated, you have to use the Route::xxxx you choose, so your HTML should be like:

    <form method="POST" action="/{{$post->slug}}">
        @csrf
    

    BUT, you have 2 equal routes (string) but pointing to the different controller's methods:

    Route::post('/{post:slug}', [PostController::class, 'create']);
    Route::post('/{post:slug}', [PostController::class, 'update']);
    

    The convention/standard here is to:

    Verb URI Action Route Name
    GET /posts index posts.index
    GET /posts/create create posts.create
    POST /posts store posts.store
    GET /posts/{post} show posts.show
    GET /posts/{post}/edit edit posts.edit
    PUT/PATCH /posts/{post} update posts.update
    DELETE /posts/{post} destroy posts.destroy

    So, you should have:

    Route::get('/posts/{post:slug}', [PostController::class, 'show']);
    Route::post('/posts/create', [PostController::class, 'create']);
    Route::put('/posts/{post:slug}', [PostController::class, 'update']);
    Route::delete('/posts/{post:slug}', [PostController::class, 'destroy']);
    

    If you have the routes above, then you have to:

    <form method="POST" action="/posts/{{$post->slug}}">
        @csrf
        @method('PUT')
    

    Read Actions Handled By Resource Controller for more information.

    More info about @method so you don't confuse what it means.