Delete method with Sweet Alert in Laravel

后端 未结 5 2102
我在风中等你
我在风中等你 2020-12-16 07:10

I\'m testing a method using Sweet Alert, to improve the messages issued by the Javascript alert method with the laravel framework.

1 - I downloaded the files sweetal

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 07:57

    You are Performing action on button click irrespective of whether you confirm or cancelled the deletion.

    Delete
    

    Jquery and Ajax:

    $(document).on('click', '.button', function (e) {
        e.preventDefault();
        var id = $(this).data('id');
        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) {
                                  //
                        }         
                });
        });
    });
    

    And:

    public function destroy(Request $request)
    {
        User::find($request->id)->delete();
        return redirect()->route('users.index')
                        ->with('success','User deleted successfully');
    }
    

提交回复
热议问题