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

behnampmdg3's avatar

Status Code: 405 Method Not Allowed message: "The PUT method is not supported for this route. Supported methods: GET, HEAD, POST."

Hi what am I doing wrong?

Status Code: 405 Method Not Allowed
message: "The PUT method is not supported for this route. Supported methods: GET, HEAD, POST."

Outputs:

<!--EDIT MODULE-->
<div class="row justify-content-center hide-this edit-module-form" id="update_module_{{ $module->id }}">

    <div class="col-md-11 alert alert-secondary mt-3 mb-3 ">
        <form class="mt-2 edit_module_form" method="post" action="/modules/{{ $module->id }}">
            @csrf @method('PUT')


            <div class="form-group">
                Title
                <input value="{{ $module->title }}" type="text" class="form-control mb-1 required" id="title" name="title" placeholder="Module Title" required> Release after how many days?

                <div class="col-4">
                    <input type="number" class="form-control required" id="release_after" name="release_after" value="{{ $module->release_after }}">
                </div>
                <input type="hidden" class="form-control" name="course_id" value="{{ $module->id }}">
            </div>
            <div class="form-group">
                <label for="module_description">Description</label>
                <textarea class="form-control" id="module_description" name="description" rows="3">{{ $module->description }}</textarea>
            </div>
            <button type="submit" class="btn btn-success btn-sml">
                        <i class="fas fa-pencil-alt"></i> Update {{ $module->title }}
                      </button>
            <button type="button" class="btn btn-warning btn-sml hide-edit-module-form">Cancel</button>
        </form>


    </div>
</div>
<!--END EDIT MODULE--> 
|        | POST      | modules                | modules.store    | App\Http\Controllers\ModuleController@store                            | web          |
|        | GET|HEAD  | modules                | modules.index    | App\Http\Controllers\ModuleController@index                            | web          |
|        | GET|HEAD  | modules/create         | modules.create   | App\Http\Controllers\ModuleController@create                           | web          |
|        | GET|HEAD  | modules/{module}       | modules.show     | App\Http\Controllers\ModuleController@show                             | web          |
|        | PUT|PATCH | modules/{module}       | modules.update   | App\Http\Controllers\ModuleController@update                           | web          |
|        | DELETE    | modules/{module}       | modules.destroy  | App\Http\Controllers\ModuleController@destroy                          | web          |
|        | GET|HEAD  | modules/{module}/edit  | modules.edit     | App\Http\Controllers\ModuleController@edit                             | web          |
//START Edit module
    $( ".edit_module_form" ).on( "submit", function( event ) 
        {
            event.preventDefault();
            $.ajaxSetup({
            headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
    var str = $( this ).serialize();
    $.ajax({
            method: "POST",
            url: "{{ url('/modules') }}",
            data: str
        })
            .done(function( msg ) {
                $( '.edit-module-form' ).hide();
                alert('Module Edited successfully!');
                location.reload();
            });     
    });
    //End Edit module 
0 likes
10 replies
Talinon's avatar

You are preventing the default action within your javascript, so it has nothing to do with the markup.

The problem is here:

$.ajax({
            method: "POST",
            url: "{{ url('/modules') }}",   <--- you only have routes for GET,HEAD,POST for /modules
            data: str
        })

You need to add your module parameter to the url

behnampmdg3's avatar

@TALINON - Sweet. Now how can I access the module_id so I add it there?

This is the result of the serialize

_token=doxmU0rTEl5jcO8STgo2dXBs3O8aptkAIG5bJz1N&_method=PUT&title=Module%201%20Course%20456&release_after=1&module_id=17&description=Desc

How can I get 17?

Thanks

Talinon's avatar

Either change your route to remove the {module} parameter and retrieve the resource within the controller via the payload, or add the id to the url

$.ajax({
            method: "POST",
            url: "{{ url('/modules/', [$module-id]) }}", 
            data: str
        })

behnampmdg3's avatar
behnampmdg3
OP
Best Answer
Level 5

@TALINON - Can't add the module id like you say!

url: "{{ url('/modules/', [$module-id]) }}", 
ErrorException (E_ERROR)
Use of undefined constant id - assumed 'id' (this will throw an Error in a future version of PHP)

I am pretty sure the best way is to access it in the result of the serlizlized as I mentioned above.

*Edit

I ended up doing this and I don't like it it's super messy. Will have to implement gates etc.

Route::put('modules', 'ModuleController@update');
public function update(Request $request, Module $module)
    {
        $module = \App\Module::where('id', $request->module_id)->first();
        if($module->user_id== \Auth::id())
            {
                $validated = $request->validate(['title' => 'required|max:255','release_after'=>'numeric']);
                \DB::table('modules')
                ->where('id', $module->id)
                ->update(['title' => $request->title,
                    'description' => $request->description,
                    'release_after' => $request->release_after
                ]);
            }
        else
            {
                echo "Not allowed";
            }    
    }
//START Edit module
    $( ".edit_module_form" ).on( "submit", function( event ) 
        {
            event.preventDefault();
            $.ajaxSetup({
            headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
    var str = $( this ).serialize();
    

    $.ajax({
            method: "POST",
            url: "{{ url('/modules/') }}",
            data: str
        })
            .done(function( msg ) {
                $( '.edit-module-form' ).hide();
                alert('Module Edited successfully!');
                location.reload();
            });     
    });
    //End Edit module 
Talinon's avatar

It was a typo. I blame that on trying to reply with a phone. But come on, you should be able to problem solve that...

url: "{{ url('/modules/', [$module->id]) }}", 

And if you think that the other way is the best, then do it. That is exactly what I said.

change your route to remove the {module} parameter and retrieve the resource within the controller via the payload

behnampmdg3's avatar

@TALINON - Thanks talinon. I am super new to all this haha.

I'll be posting more there is this thing called gates apparently the controller file looks shetty

tavdesign's avatar

try to change:

@method('PUT') with

<input type="hidden" name="__method" value="PUT">

Please or to participate in this conversation.