Confirm deletion with Sweet alert2 and Laravel 7?

Asked

Viewed 27 times

1

I have a button:

<button class="btn-floating red btn-small" data-id="{{ $tag->id }}" data-action="{{url("/admin/tags/{$tag->id}") }}" onclick="deleteConfirmation({{$tag->id}})">
   <i class="material-icons red">delete</i>
</button>

That sends to a function in Javascript, which is like this:

function deleteConfirmation(id) {

  swal.fire({
    title: "Excluir?",
    text: "Isso não poderá ser desfeito!",
    icon: "warning",
    showCancelButton: !0,
    confirmButtonText: "Sim, excluir!",
    cancelButtonText: "Não, cancelar!",
    reverseButtons: !0
  }).then(function (e) {
    if (e.value === true) {
      var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
      console.log(CSRF_TOKEN)

      $.ajax({
        type: 'DELETE',
        url: "/admin/tags/"+id,
        data: {_token: CSRF_TOKEN},
        dataType: 'JSON',
        success: function (results) {

          if (results.success === true) {
            swal("Done!", results.message, "success");
          } else {
            swal("Error!", results.message, "error");
          }
        }
      });

    } else {
      e.dismiss;
    }

  }, function (dismiss) {
    return false;
  })
}

But when shipping is giving way not allowed. I believe it is not a matter of routes. But by guarantee the routes here:

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function () {
    Route::resource('/posts', 'PostController');
    Route::put('/posts/{post}/publish', 'PostController@publish')->middleware('admin');
    Route::resource('/categories', 'CategoryController', ['except' => ['show']]);
    Route::resource('/tags', 'TagController', ['except' => ['show']]);
    Route::resource('/comments', 'CommentController', ['only' => ['index', 'destroy']]);
    Route::resource('/users', 'UserController', ['middleware' => 'admin', 'only' => ['index', 'destroy']]);
});

When I change my method to POST it gives the same method error not allowed but in the allowed methods it from that delete is one of them. I already printed the token too and it pulls normally.

I noticed that when I click confirm it comes first a Found:

inserir a descrição da imagem aqui

After the mistake comes:

inserir a descrição da imagem aqui

My goal would be to create something that requires confirmation before deleting. I tried to create something by the controller but had problems on the .then then I decided to follow the ajax and javascript that was the closest to work.

The problem here is that in addition to not being working I will have to create a class like this for each different url to request, or there is an easier way?

  • When you use Route::Resource, Laravel sets the route DELETE with a parameter {id}. For some reason, the call from your DELETE is without the id in the end.

  • Yes, that’s right! Before leaving as I put it here I had done so here: url: "{{url('/admin/tags')}}/" + id, but still without success

  • Why don’t you use the data instead of id? I haven’t seen what you’ve got before, but I guess I can see that you’re in danger of being overwritten. Try url: $(this).data('url') and remove the onclick, adding the $('.btn-deletar').on('click', deleteConfirmation)....

No answers

Browser other questions tagged

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