Zubair
Zubair

Reputation: 295

Confirmation delete data using sweetAlert with laravel 6

I'm trying to create delete confirmation in my app using sweet alert when I click to delete button so I am facing an error on console Uncaught TypeError: Cannot call a class as a function

Please suggest on how to fix this error?

error image

Controller

                  public function destroy($id)
                  {
                  $delete = Digitizing::where('id', $id)->delete();
                  if ( $delete == 1) {
                  $success = true;
                  $message = "User deleted successfully";
                  } else {
                  $success = true;
                  $message = "User not found";
                  }        
                  }

HTML view

                           <a class="btn btn-danger" onclick="deleteConfirmation({{$digitizing- 
                           >id}})">Delete</a>



                             <script type="text/javascript">
                             function deleteConfirmation(id) {
                             swal({
                            title: "Delete?",
                            text: "Please ensure and then confirm!",
                            type: "warning",
                            showCancelButton: !0,
                            confirmButtonText: "Yes, delete it!",
                           cancelButtonText: "No, cancel!",
                           reverseButtons: !0
                           }).then(function (e) {

                         if (e.value === true) {
                         var CSRF_TOKEN = $('meta[name="csrf- 
                        token"]').attr('content');

                         $.ajax({
                         type: 'POST',
                         url: "{{url('/delete')}}/" + 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;
                           })
                           }
                           </script>

Upvotes: 1

Views: 1196

Answers (1)

Cashtig
Cashtig

Reputation: 31

It's because of the swal({ part of your code. Swal is the name of the class. You will want to replace it with Swal.fire(.

Additionaly, please note that your php code will return $success = true; in both scenarios. You might want tot update that as well.

Upvotes: 2

Related Questions