Laravel Error | Methodnotallowedhttpexception

Asked

Viewed 204 times

0

I am trying to send a form to update certain record in Baco, but it displays the error message:

Methodnotallowedhttpexception

I am using route Resources, my route is difinida:

Route::resource('tasks', 'TaskController');

And my form is set as follows:

<!-- Modal to check a task -->
<div class="modal fade" id="check" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="exampleModalLabel">Atenção!</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form method="post" action="{{ route('tasks.update',['tasks' => $task->id]) }}">
                    {{ method_field('put') }}
                    {{ csrf_field() }}
                    <p>
                        Você deseja concluir esta tarefa?
                    </p>
                    <input name="code" id="code" type="hidden" value="1">
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Sair</button>
                        <button type="submit" class="btn btn-success">Concluir</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

In Chrome’s Inspect, the modal is being rendered this way:

<div class="modal fade in" id="check" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" style="display: block; padding-right: 17px;">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="exampleModalLabel">Atenção!</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="http://localhost:8000/tasks" method="POST">
                    <input type="hidden" name="_method" value="PUT">                        <input type="hidden" name="_token" value="qhXD31cYFFfSf8pq5xFGtBrVYMAuSLmzq3ExFHt4">                        <p>
                        Você deseja concluir esta tarefa?
                    </p>
                    <input name="code" id="code" type="hidden" value="1">
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Sair</button>
                        <button type="submit" class="btn btn-success">Concluir</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

In the Network tab, the return of the request is:

  • Request URL: http://localhost:8000/tasks
  • Request Method: POST
  • Status Code: 405 Method Not Allowed
  • Remote Address: 127.0.0.1:8000
  • Referrer Policy: no-referrer-when-downgrade
  • Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
  • Accept-Encoding: gzip, deflate, br
  • Accept-Language: en-US,en;q=0.9
  • Cache-Control: max-age=0
  • Connection: Keep-Alive
  • Content-Length
  • Content-Type: application/x-www-form-urlencoded
  • Cookie: XDEBUG_SESSION=PHPSTORM; XSRF-TOKEN=;
  • laravel_session=eyJpdiI6IjVCRnYrcENYRTlieEFtN3BvbFpjOVE9PSIsInZhbHVlIjoiN21wXC81Vk4yZGUwN3FmR1Vzc1k0bjk4R2tkUUs4V3ZiWndYRUVSTFZJdkVlY3VFM0xlVUluRVo3c0xTTWk0M0oiLCJtYWMiOiI3OGMxMjUzZmJmODgxNjg0NDA0ODA0ZDM3Nzk3MWIwMWE0MjFjOGIwMzk0ZmE0OWJkY2MwMmRhNjZlZTY0MGJhIn0%3D
  • Host: localhost:8000
  • Origin: http://localhost:8000
  • Referer: http://localhost:8000/Projects/1
  • Upgrade-Insecure-Requests: 1
  • User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Applewebkit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
  • _method: PUT
  • _token: qhXD31cYFfSf8pq5xFGtBrVYMAuSLmzq3ExFHt4
  • code: 1
  • You can include in your question how the rendered html of this view looks?

  • you would like to see the error screen that was rendered or the rest of my html?

  • the generated html before giving Submit

  • @gmsantos entered as requested. However, the code is inside a file where I am placing all the modal’s of the application.

  • Try switching to {{ method_field('PATCH') }}

  • @caused this change and also remained the error.

Show 1 more comment

1 answer

0

In the Laravel, the request method is identified by the attribute _method.

If you want to access the @store Controller, you can simply send a POST, however, if you want to access the @update you will need to inform this with a Hidden input.

If it’s not working the way you included it, you can choose to create the input manually like this:

<input type="hidden" name="_method" value="PUT"/>

Or, use the tag of Lade, depending on your version of Laravel: {{ @method('PUT') }}

Tips for debug:

To check if this is working, enter the form after it is rendered, so you will understand if the Hidden input is being generated as it should.

Another alternative that really helps me a lot: In your browser’s Network tab, click to keep browsing history (this will allow you to see the request even if you have already switched pages). This way, you can submit the form and inspect the package exactly as it was sent.

Edit:

I just saw that your form rendering has incorrect action: http://localhost:8000/tasks. A PUT requires the {id} en route.

Try rendering like this:

action="tasks/{{ $task->id }}"
  • I followed your tips, input all three ways: <input type="hidden" name="_method" value="PUT"/> / {{ @method('PUT') }} / {{ method_field('PUT') }} and for all of them the result of the error was the same :/

  • @Gustavo_tavares, then go to your browser’s Inspect and let’s understand how the form and request are being generated

  • Days, grateful for your help dear. I edited my question and added the Inspection and the return of the request.

  • @Gustavo_tavares just to confirm, your controller has a method update(), correct?

  • @Gustavo_tavares, it may seem banal, but test PATCH in place of PUT to see.

  • @Gustavo_tavares, now I think it will work, see my Edit.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.