Error Laravel | MethodNotAllowedHttpException

0

I'm trying to send a form to update a certain record in the baco, but it displays the error message:

  

MethodNotAllowedHttpException

I'm using route resources, my route is defined:

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

And my form is defined 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 the Chrome 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, at; q = 0.9
  • Cache-Control: max-age = 0
  • Connection: keep-alive
  • Content-Length: 66
  • Content-Type: application / x-www-form-urlencoded
  • Cookie: XDEBUG_SESSION = PHPSTORM; XSRF-TOKEN = eyJpdiI6IlBBSnFZSm5tZTJseUhxZDlIdVRrMGc9PSIsInZhbHVlIjoidHRvN3RTaStzcEFjck1veDJWa2k0cVwvdkxsalRcL01hYmpIOFhIS2xkeXFUczU1RTFzOTJqMGRZMGJSSGJaNlZ6IiwibWFjIjoiZjBkOGE0NzJjNDQ3OTI0ZmFjYWU0MDA3MmMxNTliMzcxODYxODM1NWFlNGVjOWU1NWEwMzM0ZmQxMDExZTkxYyJ9;
  • 3D laravel_session = eyJpdiI6IjVCRnYrcENYRTlieEFtN3BvbFpjOVE9PSIsInZhbHVlIjoiN21wXC81Vk4yZGUwN3FmR1Vzc1k0bjk4R2tkUUs4V3ZiWndYRUVSTFZJdkVlY3VFM0xlVUluRVo3c0xTTWk0M0oiLCJtYWMiOiI3OGMxMjUzZmJmODgxNjg0NDA0ODA0ZDM3Nzk3MWIwMWE0MjFjOGIwMzk0ZmE0OWJkY2MwMmRhNjZlZTY0MGJhIn0%
  • 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: qhXD31cYFFfSf8pq5xFGtBrVYMAuSLmzq3ExFHt4
  • code: 1
asked by anonymous 01.12.2018 / 14:21

1 answer

0

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

If you want to access the @store of the Controller, you can simply send POST , but if you want to access @update you need to enter this with an input hidden.

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

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

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

Tips for debugging:

To verify that this is working, inspect the form after rendering, so you will understand if the input hidden is being generated as it should.

Another alternative that really helps me a lot:  On the Network tab of your browser's developer tools, click to maintain your browsing history (this will allow you to see the request even though you have already changed pages). That way you can submit the form and inspect the package exactly as it was submitted.

Edit:

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

Try rendering like this:

action="tasks/{{ $task->id }}"
    
03.12.2018 / 13:35