Saturday, July 25, 2020

Ajax delete with sweetalert & toastr message using laravel ??


HTML:

@foreach($user-> as $key => $row)
                        <tr class="{{$row->id}}">
                            <td>
                                 <a class="deleteAddress" data-id="{{ $row->id }}" title="Delete Shipping Address"><i class="fa fa-trash" aria-hidden="true"></i></a>
                            </td>    
                        </tr>

@endforeach

web.php:

Route::get('/delete-shipping-address/{id}', 'Website\AccountController@delete_shipping_address')->name('delete-shipping-address');

Controller:

 public function delete_shipping_address($id){
      $shipping_address = Address::findOrFail($id);
      $shipping_address->delete();
      return response()->json(['status' => 'success', 'message' => 'Delete Address successfully!']);


  }



css :

<link rel="stylesheet" href="{{ asset('backoffice/global_assets/bootstrap-sweetalert/dist/sweetalert.css') }}">

 <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.css">

JS:

<script src="{{ asset('backoffice/global_assets/bootstrap-sweetalert/dist/sweetalert.min.js') }}"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>


<script type="text/javascript">
    $(function(){
       $('.deleteAddress').click(function() {

            var id=$(this).data('id'); 
            var url = "{{URL('delete-shipping-address')}}";
            var dltUrl = url+"/"+id;

            swal({
              title: "Are you sure?",
              text: "Press Yes to confirm or press No to cancel.",
              type: "warning",
              showCancelButton: true,
              confirmButtonClass: "btn-danger",
              confirmButtonText: "Yes",
              cancelButtonText: "No",
              closeOnConfirm: true,
              closeOnCancel: true
            },
            function(isConfirm){
              if(isConfirm){

                $.ajax({
                    url: dltUrl,
                    type: 'GET',
                    data: { 
                        id:id,
                        _token: "{{ csrf_token() }}"
                    },
                    success: function(data){ 
                        if (data.status == 'success') {
                            toastr.success(data.message); 
                        }  

                        location.reload(); 
                    },
                   
                });
             
              }
            });

       }); 
       
    });

    $(function(){
        toastr.options = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-top-right",
        "onclick": null,
        "showDuration": "1000",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }

    @if(Session::has('message'))
        var type = "{{ Session::get('alert-type', 'info') }}";
        switch(type){
            case 'info':
                toastr.info("{{ Session::get('message') }}");
                break;

            case 'warning':
                toastr.warning("{{ Session::get('message') }}");
                break;
            case 'success':
                toastr.success("{{ Session::get('message') }}");
                break;
            case 'error':
                toastr.error("{{ Session::get('message') }}");
                break;
        }
    @endif  
    });

</script>

No comments:

Post a Comment

Ajax load lage with laravel.

 step-1:  HTML <div class="row">                     <div class="col-lg-12">                           <d...