Search code examples
laravelyajra-datatable

How to fix "The GET method is not supported for this route. Supported methods: PUT."


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
            }
        ]
    });
});

Solution

  • 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() }}\">"