4
votes

When I submit a request to update some data, I get this error "The GET method is not supported for this route. Supported methods: PUT.". How to get rid of this?

Here I've added codes of web.php, AdminController.php and JS function to populate datatable.

route:

Route::group(['prefix' => '/admins'], function () {
    Route::get('/show', [
        'uses' => 'AdminController@show',
        'as'   => 'admins.show',
    ]);

    Route::put('/approve/{id}',     [
        'uses' => 'AdminController@approve',
        'as'   => 'admins.approve',
    ]);
});

AdminController:

public function show()
{
    return Datatables::of(User::query()->whereNotNull('email_verified_at'))->make(true);
}

public function approve(Request $request, $id)
{
    $user = User::find($id);
    $user->approved_by = Auth::user()->name;
    $user->approved_at = new \DateTime();

    $user->save();

    return redirect('/admins/show');
} 

Datatable function:


$(function () {
    $('#admins').DataTable({
        processing: true,
        serverSide: true,
        autoWidth: true,
        scrollX: true,
        order: [[1, "asc"]],
        pagingType: "full_numbers",
        ajax: '{{ url('admins/ show') }}',
        columns: [
            { data: 'id', name: 'id' },
            { data: 'name', name: 'name' },
            { data: 'email', name: 'email' },
            { data: 'email_verified_at', name: 'email_verified_at' },
            { data: 'approved_by', name: 'approved_by' }
        ],
        columnDefs: [
            {
                targets: 0,
                visible: false,
                searchable: false
            },
            {
                targets: 5,
                render: function (data, type, row, meta) {
                    if (row.approved_by === null) {
                        return "<form action=\"/admins/approve/" + row.id + "\" method=\"put\"><input type=\"submit\" class=\"btn btn-success\" value=\"Approve\"></form><button type=\"button\" class=\"btn btn-danger\">Delete</button>";
                    } else {
                        return "<button type=\"button\" class=\"btn btn-primary\">Reject</button><button type=\"button\" class=\"btn btn-danger\">Delete</button>";
                    }
                },
                className: "col-action",
                searchable: false,
                orderable: false
            }
        ]
    });
});
2

2 Answers

2
votes

HTTP verbs like PUT are generally not supported by the webservers, Laravel achieves this using method spoofing, so you'll need to pass an input type=hidden specifying the method you want to use. And your form action needs to "POST" for the same reason.

 return "<form action=\"/admins/approve/" + row.id + "\" method=\"POST\">
               <input type=\"hidden\" name=\"_method\" value=\"PUT\">"

Also make sure you are passing the csrf-token with your POST requests.

<input type=\"hidden\" name=\"_token\" value=\"{{ csrf_token() }}\">"

0
votes
  1. In laravel, you need to send a key _method with value PUT(case insensitive) to be able to tell laravel that you are sending request with put method. more info
  2. Since your route is in web.php and your method is not GET, you need to send a csrf token with the request. Send a key _token with value of csrf token. You can do this by default for all the $.ajax requests. Use this code.

Make these changes in your render method and you will be good to go.

render: function(data, type, row, meta) {
  if (row.approved_by === null) {
    return "<form action=\"/admins/approve/" + row.id + "\" method=\"post\"><input type=\"hidden\" name=\"_method\" value=\"PUT\"><input type=\"submit\" class=\"btn btn-success\" value=\"Approve\"></form><button type=\"button\" class=\"btn btn-danger\">Delete</button>";
  } else {
    return "<button type=\"button\" class=\"btn btn-primary\">Reject</button><button type=\"button\" class=\"btn btn-danger\">Delete</button>";
  }
},

I have not included code to send csrf token. You can do it for the whole application using this link.