使用laravel 5.3和jquery删除表行

时间:2017-08-08 10:49:25

标签: jquery laravel-5 modal-dialog laravel-5.3

我有用户表,每行包含一个删除用户行的按钮。  按钮上的clic显示删除确认的模式。 但尝试删除行时仍然有此错误。  MethodNotAllowedHttpException

这是模态代码:

    @Override
        public View getView(final int position,  View convertView, ViewGroup parent) {
            Log.e("sara" , "this part takes time");


            LayoutInflater inflater = getLayoutInflater();


            convertView = getLayoutInflater().inflate(R.layout.gallery_gridsq, parent, false);
            iv = (ImageView) convertView.findViewById(R.id.icon);
            file = new File(Uri.parse(getItem(position).toString()).getPath());
            Uri imageUri = Uri.fromFile(file);
            Glide
                    .with(Gallery2.this)
                    .load(imageUri)
                    .into(iv);


            return convertView;
        }

我的jquery代码:

<div class="modal fade " id="modal-delete">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        <div class="modal-body">
          <p>Voulez-vous supprimer cet utilisateur ?</p>
        </div>
        <div class="modal-footer">
    <!------------------------
          <a href='/SurveyApp/public/admin/{{ $user->id }}/delete' class="btn btn-primary" data-dismiss="modal" class=" modal-action waves-effect waves-light btn-flat red-text">oui</a>
    ---------------->
  <form method="POST" action="/SurveyApp/public/admin/{{ $user->id }}/delete" id="delete-court-form">
              <input type="hidden" name="_token" value="{{ csrf_token() }}">
              <input type="hidden" name="_method" value="DELETE">
              <button type="button" class="btn btn-default"
                      data-dismiss="modal">Fermer</button>
           <!--  <a href='/SurveyApp/public/admin/{{ $user->id }}/delete' class="btn btn-danger">oui!</a> -->
                         <button type="submit" class="btn btn-danger" id="delete-court-button" >
                <i class="fa fa-times-circle"></i> Oui
              </button>

            </form>

和控制器中的方法:

<script>
$('.delete-court-button').on('click', function () {
    $('#delete-court-form').attr('action', $(this).data('delete-link'));
});
</script>

2 个答案:

答案 0 :(得分:0)

  1. 您使用DELETE方法发送表单,但在路由定义中使用GET。它们应该是一样的。
  2. 使用route()帮助程序创建删除链接
  3. 所以你的表格应该是这样的:

        <form method="POST" action="{{route('delete.user',$user->id)}}" id="delete-court-form">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <input type="hidden" name="_method" value="DELETE">
            <button type="button" class="btn btn-default"
                data-dismiss="modal">Fermer
            </button>
            <button type="submit" class="btn btn-danger" id="delete-court-button">
                <i class="fa fa-times-circle"></i> Oui
            </button>
        </form>
    

    您的路线定义:

    Route::delete('/admin/{user}/delete','AdminController@delete')->name('delete.user')->middleware('is_admin');
    

    你的控制器方法:

    public function delete($user_id){
        $user = User::findOrFail($user_id);  
        $surveys=Survey::where('user_id',$user->id)->get(); 
            $user->delete();
        foreach($surveys as $survey){
        $survey->delete();
        }
    
        return redirect('/admin/user');
        }
    

答案 1 :(得分:0)

检查你的路线

示例:  路线:柱( '/管理/ {用户} /删除', 'AdminController @删除')

表单和路由方法都应该有相同的方法(“POST”)