在 laravel 中使用模态框删除数据

Delete data using modal box in laravel

我在 laravel 中删除确认数据(在本例中使用模式框)时遇到一些问题。

这是我的删除按钮

{{ Form::open(array(
                                'route' => array('delete_spk', $spk_data->id),
                                'method' => 'put',
                                'style' => 'display:inline'
                                ))
                            }}
                            <button class="btn btn-danger btn-line btn-rect" type="submit" data-toggle="modal" data-target="#delSpk" data-title="Delete SPK" data-message='Are you sure you want to delete this data ?'>
                            <i class="icon-trash icon-white"></i> Delete</button>
                            {{ Form::close() }}

这是模态框

<!--MODAL DELETE SPK-->
<div class="col-lg-12">
                        <div class="modal fade" id="delSpk" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title" id="H4"> Delete SPK</h4>
                                        </div>
                                        <div class="modal-body">
                                        <p class="help-block">Are you sure you want to delete this data ?</p>
                                        </div>
                                        <div class="modal-footer">
                                       <button type="button" class="btn btn-danger btn-line btn-rect" id="confirm">Yes</button>
                                        <button type="button" class="btn btn-primary btn-line btn-rect" data-dismiss="modal">No</button>
                                        </div>

                                    </div>
                                </div>
                            </div>
                        </div>
<!--END OF MODAL DELETE SPK-->

<!-- Dialog show event handler -->
<script type="text/javascript">
  $('#delSpk').on('show.bs.modal', function (e) {
      $message = $(e.relatedTarget).attr('data-message');
      $(this).find('.modal-body p').text($message);
      $title = $(e.relatedTarget).attr('data-title');
      $(this).find('.modal-title').text($title);

      // Pass form reference to modal for submission on yes/ok
      var form = $(e.relatedTarget).closest('form');
      $(this).find('.modal-footer #confirm').data('form', form);
  });

  <!-- Form confirm (yes/ok) handler, submits form -->
  $('#delSpk').find('.modal-footer #confirm').on('click', function(){
      $(this).data('form').submit();
  });
</script>

这是路线

Route::get('spk/destroy/{id}', array('as'=>'delete_spk','uses'=>'SpkController@destroy'));

这是删除数据的控制器

public function destroy()
 {
  $spk= Spk::find(Input::get('id'))->delete();
        Session::flash('message', 'Successfully deleted the SPK !');
        return Redirect::to('spk_view');
 }

模态框可以正常工作,但是当我获取要删除的 ID 时,这将以结果 "method not allowed http exception" 结束。有谁可以帮助我吗?

出现此问题是因为您将路由定义为 GET 但将表单提交为 PUT

如果您按如下方式定义路由,那么它一定可以工作

Route::put('spk/destroy/{id}', array('as'=>'delete_spk','uses'=>'SpkController@destroy'));