How do I implement Sweet Alert by RealRashid to confirm Record Delete in controller destroy() method?

Viewed 39
public function destroy(Company $company)
{
    Alert::question('Delete Record?', 'Cannot Undo! Are You Sure?');

    if (session('status')) {
        $company->delete();
    }
    return back()->with('status', 'Company Deleted!');
}

At the moment the record deletes with or without the Sweet Alert confirmation. I want the record deleted only after the Sweet Alert confirmation is clicked.

1 Answers

Just change button type from submit to button and trigger vai javascript function

<form action="{{ route('orders.destroy', $row->id) }}" method="post" class="d-inline">@csrf@method('DELETE')<button type="button" class="btn btn-sm btn-danger confirm-delete"><i class="fas fa-times"></i></button></form>

$(document).on('click', 'button.confirm-delete', function () {
Swal.fire({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then((result) => {
  if (result.isConfirmed) {
    $(this).parent('form').trigger('submit')
  } else if (result.isDenied) {
    Swal.fire('Changes are not saved', '', 'info')
  }
});

});

Related