2016-08-01 5 views
0

コメント用にリソースコントローラを使用しています。 ブレードのコメントを削除するリンクがあります。laravelのjqueryを使用してリソースコントローラのdestroyメソッドにリダイレクトすることはできますか?

<a href="" ><span class="glyphicon glyphicon-trash delete"></span></a> 

私はそれにjqueryのダイアログ・ポップ・アップをクリックします。ダイアログボックスのコードは次のようになります。

jQuery(document).ready(function($){ 
    $('.edit-delete span.delete').click(function(e){ 
     e.preventDefault(); 
     $('<div id="dialog" class="pull-center"></div>').appendTo('body').html('<div"><h4>Are you sure you want to delete this comment?</h4></div>') 
     .dialog({   
      autoOpen: true, 
      modal : true, 
      title : 'Confirm', 
      buttons: { 
       "Yes" : function(){      
        $(this).dialog('close'); 
        $(location).attr("href", " {{what do i put here?}} "); 

       }, 
       "No" : function(){ 
        $(this).dialog('close'); 
       } 
      } 
     }); 


    }); 

}); 

しかし、我々はそれを達成するためにmethod.Howを削除使用する必要として機能しませんリダイレクト驚き?あなたはlaravelルートへのリンクをお勧めしますあなたの{{what do i put here?}}セクションで

答えて

0

、この

{{ route('routeName', ['id' => $comment->id]) }} 

のようなものが、これはしかし、それはより正常であるかもしれない、GETメソッドを使用する必要がありますhttps://laravel.com/docs/5.2/routing#named-routes

ノートを参照してくださいフォームを使用して、PUTまたはDELETEを使用することができます。この場合、この意味では意味があります。あるいは、この種の要求からAJAXを使用することがよくあります。

0

https://gist.github.com/JeffreyWay/5112282 を使用すると、リンクを使用してコメントなどを削除できます。あなたのヘッダーのどこかに上記のリンクで提供し、あなたのビューにHTMLリンクを配置れるjavascriptを含める:

<a href="delete/{{$id}}(or whatever delete route you have)" data-method="delete" data-token="{{csrf_token()}}" data-confirm="Are you sure you want to delete this x ?"> 

を次に、あなたのルートが使用できます

Route::delete('delete/{id}','[email protected]_delete_method'); 
0

解決策を見つけました。 csrfトークンについては、以下のようにhtmlに隠し入力フィールドを作成する必要があります。

<input type="hidden" id="token" name="_token" value="{{ csrf_token() }}"> 

以下のようにhrefでタグを作成する必要があります。

<a class="js-ajax-delete" href="{{route('comment.destroy', $comment->id)}}" ><span class="glyphicon glyphicon-trash"></span></a> 

スクリプトは次のようになります。

jQuery(document).ready(function($){ 

    $('.js-ajax-delete').click(function(e){ 
     e.preventDefault(); 
     var deleteUrl = $(this).attr('href'); 
     var token = $('#token').val(); 

     $('<div id="dialog" class="pull-center"></div>').appendTo('body').html('<div"><h4>Are you sure you want to delete this comment?</h4></div>') 
     .dialog({ 

      autoOpen: true, 
      modal : true, 
      title : 'Confirm', 
      buttons: { 
       "Yes" : function(){ 

        $(this).dialog('close'); 

        $.ajaxSetup({ 
          headers: { 
           'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') 
          } 
         }); 

        //Delete request 
         $.ajax({ 
          type: 'DELETE', 
          url: deleteUrl, 
          data: { _token :token }, 
          success: function(data){ 
           if (data == "true") { 
           window.location.href=window.location.href; 
           }; 
          } 
         }); 
       }, 
       "No" : function(){ 
        $(this).dialog('close'); 
       } 
      } 
     });   

    }); 
}); 

とコントローラ内の削除方法は次のようになります:私たちはjQueryのをロードする必要がダイアログボックスを実行するために

public function destroy($id) 
    { 
     $comment = Comment::find($id); 
     $comment->delete(); 
     session()->flash('success', 'Comment has been deleted'); 
     return "true";//it has to be a string 
    } 

-UIとjQueryの併用 誰かを助けることを願っています。

関連する問題