AhmedMamdouh's avatar

Implement sweet alert delete

now i have a sweet alert button for delete how can i implement it so as to when i click on the confirmation message it delete the record? i don't know javascript so i need a simplified way to implement it

0 likes
4 replies
tykus's avatar

Adapted from the SweetAlert docs:

swal({
    title: "Are you sure?",
    text: "Once deleted, you will not be able to recover this!",
    icon: "warning",
    buttons: true,
    dangerMode: true,
})
.then((willDelete) => {
    if (willDelete) {
        axios.delete('/path/to/delete/resource')
            .then((response) =>  {
                swal("Deleted successfully!", {
                        icon: "success",
                    });
            })
            .catch(() => {
                swal("Error!", "Failed to delete!", "error");
            })
    }
})
1 like
AhmedMamdouh's avatar

i mean implement in controller and ajax that when i click on accept it deleted from database

devit.gg's avatar

This has already been answered on stackoverflow which means you havn't googled before asking. Next time, please google first!

    swal({
            title: "Are you sure!",
            type: "error",
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Yes!",
            showCancelButton: true,
        },
        function() {
            $.ajax({
                type: "POST",
                url: "{{url('/destroy')}}",
                data: {id:id},
                success: function (data) {
                              //
                    }         
            });
public function destroy(Request $request)
{
    User::find($request->id)->delete();
    return redirect()->route('users.index')
                    ->with('success','User deleted successfully');
}
1 like

Please or to participate in this conversation.