如何通过ajax删除laravel中的记录

时间:2017-06-15 14:54:10

标签: php ajax laravel

我知道有关于这个问题的类似问题,但我已经在那些有正确答案的人之后尝试了我的副本,但仍无济于事。 我一直收到这个错误:

  

BadMethodCallException删除方法不存在。在Macroable.php中(第74行)

要快点,这是我的控制器:

public function destroy(Subject $subject)
{
    //
    $response = array();

    $modal = new Subject; 
    $modal = Subject::find($subject);

    if ( $modal->delete() ) {
        $response['success'] = '<b>'.$modal->name.'</b>'.' successfully deleted';
        $response['subject'] = $modal;   
    }

    return \Response::json($response);

}

这是我的路线:

Route::delete('/subjects/delete/{subject}', 'SubjectsController@destroy');

以下是我的观点:

<td>
    <a data-token="{{ csrf_token() }}" id="delete" data-id="{{$subject->id}}" data-toggle="tooltip" title="Edit" href="/subjects/{{$subject->id}}" role="button"><i class="glyphicon glyphicon-trash text-danger"></i></a>
</td>

并持续我的脚本:

$(document).on('click', '#delete', function(event) {
            event.preventDefault();
            /* Act on the event */

            // id of the row to be deleted
            var id = $(this).attr('data-id');

            var token = $(this).data("token");

            console.log(id);

            // row to be deleted
            var row = $(this).parent("td").parent("tr");

            var message = "subject";

             bootbox.dialog({
                message: "Are you sure you want to Delete this "+message+"?",
                title: "<i class='glyphicon glyphicon-trash'></i> Delete !",
                buttons: {
                    success: {
                        label: "No",
                        className: "btn-success",
                        callback: function() {
                            $('.bootbox').modal('hide');
                        }
                    },
                    danger: {
                        label: "Delete!",
                        className: "btn-danger",
                        callback: function() {

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

                            $.ajax({
                                type: 'DELETE',
                                url: '/subjects/delete/'+id,
                                data: {
                                    "id": id,
                                    "_method": 'DELETE',
                                    "_token": token
                                }
                            })
                           .done(function(response){
                                bootbox.alert(response.success);
                                //removing the row that have been deleted
                                jQuery(row).fadeOut('slow');
                            })
                           .fail(function(){
                                bootbox.alert('Something Went Wrong .... Please contact administrator');
                            })
                        }
                    }
                }
            });
        });

这是我在php artisan route:list

时得到的结果

enter image description here

2 个答案:

答案 0 :(得分:1)

将ajax调用中的类型更改为POST,数据对象中的_method字段就是所需的全部内容。这就是你的ajax调用的样子。 Laravel通过使用_method字段确定http动词来“伪造”DELETE方法。

$.ajax({
    type: 'POST',
    url: '/subjects/delete/'+id,
    data: {
        "_method": 'DELETE',
        "_token": token
    }
})
.done(function(response){
    bootbox.alert(response.success);
    //removing the row that have been deleted
    jQuery(row).fadeOut('slow');
})
.fail(function(){
    bootbox.alert('Something Went Wrong .... Please contact administrator');
})

答案 1 :(得分:1)

我不知道为什么它以这种方式工作的技术细节,但似乎问题来自我的控制器销毁方法。所以,这就是我必须做的所有事情:

控制器代码:

new

脚本:

public function destroy($subject)
{
    //
    $response = array();

    $modal = new Subject; 
    $modal = Subject::find($subject);

    if ( $modal->delete() ) {
        $response['success'] = '<b>'.$modal->name.'</b>'.' successfully deleted';
        $response['subject'] = $modal;   
    }

    return \Response::json($response);

}

路线:

callback: function() {

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

                            $.ajax({
                                type: 'DELETE',
                                url: '/subjects/delete/'+id,
                                data:{"id": id, "_method": 'DELETE'}
                            })
                           .done(function(response){
                                bootbox.alert(response.success);
                                //removing the row that have been deleted
                                jQuery(row).fadeOut('slow');
                            })
                           .fail(function(){
                                bootbox.alert('Something Went Wrong .... Please contact administrator');
                            })
                        }

对我来说最重要的是从Route::delete('/subjects/delete/{subject}','SubjectsController@destroy'); 参数中删除Subject并且它有效。 如果你们中的任何人知道它为什么有效,请提供和解释,以便我也能理解。谢谢!