2017-05-07 21 views
1

私はLaravelのSweet Alertで削除モーダルを作成しました。これは私が選択したユーザーを削除しています。しかし、削除後に私のdestroy()メソッドが言うように、ユーザーリストにリダイレクトするのが好きです。ここでSweet Alertリダイレクトを使用したLaravel

<script> 
    $('a#delete_user').on('click', function() { 
     var url = $(this).attr("data-href"); 
     swal({ 
       title: "Delete user?", 
       text: "Submit to delete", 
       type: "warning", 
       showCancelButton: true, 
       closeOnConfirm: false, 
       confirmButtonColor: "#DD6B55", 
       confirmButtonText: "Delete!" 
      }, 
      function() { 
       setTimeout(function() { 
        $.ajax({ 
         type: "POST", 
         url: url, 
         data: { 
          _method: 'DELETE', 
          _token: csrf_token 
         }, 
         success: function (data) { 
          if (data) 
           swal("Deleted!", "User has been deleted", "success"); 
          else 
           swal("cancelled", "User has not been deleted", "error"); 
         } 
        }), 2000 
       }); 
      }); 
    }) 
</script> 

がコントローラである:

public function destroy($id) 
{ 
    User::destroy($id); 
    return redirect('users')->with('status', 'User Deleted!'); 
} 

私は、メッセージをユーザーリストにリダイレクトしたい、と私はトラブルあなたのメインのテンプレートファイルでは、ビューファイルでそう

答えて

0

をやってを持っています、セッションにステータスデータがある場合はをチェックする必要があります。はいの場合はスイートアラートを呼び出します。コード例:

@if (session('status')) 
    <script> 
     $(document).ready(
      swal("{{ session('status') }}") 
     }); 
    </script> 
@endif 
+0

は、それがトリガーされません。そして、 'location.reload()'を設定した場合、ステータスは転送されません。 – Norgul

+0

トリガされていないものをチェックする必要があります - 甘いアラートコールかセッション( 'status')が空です。 – sebbz

+0

私の解決策はあなたがユーザー削除ボタンでajaxリクエストを削除する場合にのみ機能します。 ajax呼び出しを使用すると、コントローラのリダイレクト部分は機能しません。 – sebbz

0

編集jsがこのようなファイル:

success: function (data) { 
if (data == 'success') 
    swal("Deleted!", "User has been deleted", "success"); 
    window.location('Your URL'); 
else 
    swal("cancelled", "User has not been deleted", "error"); 
} 

とコントローラ:私はすでにそれを持っている

if(User::destroy($id)){ 
    return 'success'; 
}else{ 
    return 'fail'; 
} 
関連する問題