Laravel 405 Method Not Allowed For Delete Request

442 views Asked by At

I make a CRUD for the country entity when I try to delete a specific element by id without encrypted cipher text. It was deleted correctly.

Axios Request

 <script>
        function confirmDestroy(id, refrance) {
            Swal.fire({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then((result) => {
                if (result.isConfirmed) {
                    destoy(id, refrance);
                }
            });
        }

        function destoy(id, refrance) {
            // static-countries/{id}
            axios.delete('/cheek-system/static-countries/', id)
                .then(function(response) {
                    // handle success
                    console.log(response);
                    refrance.closest('tr').remove();
                    showDeletingMessage(response.data);
                })
                .catch(function(error) {
                    // handle error
                    console.log(error);
                    showDeletingMessage(error.response.data);
                })
                .then(function() {
                    // always executed
                });
        }

        function showDeletingMessage(data) {
            Swal.fire({
                icon: data.icon,
                title: data.title,
                text: data.text,
                showConfirmButton: false,
                timer: 2000
            });
        }
    </script>

Country Delete Item

 @can('Delete-Country')
         <button type="button" onclick="confirmDestroy({{ $country->id }}', 
              this)" class="btn btn-danger">
         <i class="fas fa-trash"></i>
        </button>
    @endcan

And delete function from the controller

// Delete Static Country
public function destroy($id)
{
    // Check Ability
    $this->checkUserAbility('Delete-Country');

    $isDeleted = DB::table('static_countries')->where('id', $id)->delete();
    if ($isDeleted) {
        return response()->json([
            'icon' => 'success',
            'title' => 'Deleted',
            'text' => 'Static country deleted successfully'
        ], Response::HTTP_OK);
    } else {
        return response()->json([
            'icon' => 'error',
            'title' => 'Failed',
            'text' => 'Failed to delete static country'
        ], Response::HTTP_BAD_REQUEST);
    }
}

When I send the same request for a server with encrypted text with Crypte I face 405 Method Not Allowed response

Country item with encrypted id

@can('Delete-Country')
     <button type="button" onclick="confirmDestroy('{{ Crypt::encrypt($country->id) }}', 
          this)" class="btn btn-danger">
     <i class="fas fa-trash"></i>
    </button>
@endcan

Axios request with encrypted id

<script>
        function confirmDestroy(id, refrance) {
            Swal.fire({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then((result) => {
                if (result.isConfirmed) {
                    destoy(id, refrance);
                }
            });
        }

        function destoy(id, refrance) {
            // static-countries/{id}
            axios.delete('/cheek-system/static-countries/', id)
                .then(function(response) {
                    // handle success
                    console.log(response);
                    refrance.closest('tr').remove();
                    showDeletingMessage(response.data);
                })
                .catch(function(error) {
                    // handle error
                    console.log(error);
                    showDeletingMessage(error.response.data);
                })
                .then(function() {
                    // always executed
                });
        }

        function showDeletingMessage(data) {
            Swal.fire({
                icon: data.icon,
                title: data.title,
                text: data.text,
                showConfirmButton: false,
                timer: 2000
            });
        }
    </script>

Destroy function from the controller

// Delete Static Country
public function destroy($country_enc_id)
{
    // Check Ability
    $this->checkUserAbility('Delete-Country');

    $isDeleted = DB::table('static_countries')->where('id', Crypt::decrypt($country_enc_id))->delete();
    if ($isDeleted) {
        return response()->json([
            'icon' => 'success',
            'title' => 'Deleted',
            'text' => 'Static country deleted successfully'
        ], Response::HTTP_OK);
    } else {
        return response()->json([
            'icon' => 'error',
            'title' => 'Failed',
            'text' => 'Failed to delete static country'
        ], Response::HTTP_BAD_REQUEST);
    }
}
1

There are 1 answers

0
Nawaf Alharbi On

If it works without encrypted cipher text.

You need to change Crypt::encrypt. because it produces slash(/). Or you can move it to the request body rather than put it on the query parameter.