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:
After the mistake comes:
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 yourDELETE
is without theid
in the end.– Wallace Maxters
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– Peterson Medeiros
Why don’t you use the
data
instead ofid
? I haven’t seen what you’ve got before, but I guess I can see that you’re in danger of being overwritten. Tryurl: $(this).data('url')
and remove theonclick
, adding the$('.btn-deletar').on('click', deleteConfirmation)
....– Wallace Maxters