Laravel Ajax使用按钮删除记录

时间:2018-11-09 10:15:28

标签: ajax laravel button

我不明白为什么它不起作用:

路线

Route::delete('/dashboard/booking/deletebooking/{id}','ResourceController@deletebooking')->name('works.deletebooking');

ResourceController

public function deletebooking($id){
    $booking = Booking::where('id','=',$id)->get();
    $booking->delete();

    return response()->json(['success' => true],200);
}

表格

<tr id="{{$booking->id}}">
    <td class="roomId">{{$booking->room_id}}</td>
    <td class="roomName">{{$booking->name}}</td>
    <td class="roomLocation">{{$booking->sede}}</td>
    <td class="start">{{$booking->start_date}}</td>
    <td class="end">{{$booking->end_date}}</td>
    <td>
        <input type="hidden" name="_method" value="delete" />
        <button class="btn btn-danger btn-xs" id="destroy" data-id="{{$booking->id}}" data-token="{{ csrf_token() }}">
            <span class="glyphicon glyphicon-trash"></span>
        </button>
    </td>
</tr>

请求Ajax

$(".btn").click(function(){
    var id = $(this).data('id');

   // var $tr = $(this).closest('tr');
    $.ajax({
                url: "/dashboard/booking/deletebooking/"+id,
                dataType: "JSON",
                type: 'POST',
                data: {
                    '_token': $('meta[name=csrf-token]').attr("content"),
                    '_method': 'DELETE',
                     "id": id
                },
                success: function ()
                {
                    console.log("it Work");
                }
            });

    console.log("It failed");
});

我有此错误:

  

请求网址:http://pickbooking.local/dashboard/booking/deletebooking/1   请求方法:POST   状态码:500内部服务器错误   远程地址:192.168.10.10:80

2 个答案:

答案 0 :(得分:0)

问题出在用于ajax调用post

的方法中
// var $tr = $(this).closest('tr');
$.ajax(
        {
            url: "/dashboard/booking/deletebooking/"+id,
            dataType: "JSON",
            type: 'POST',
            data: {
                '_token': $('meta[name=csrf-token]').attr("content"),
                '_method': 'DELETE',
                 "id": id
            },
            success: function ()
            {
                console.log("it Work");
            }
        });

数据将在请求的正文中发送,而在DELETE请求中则没有正文。因此laravel不会看到_method_token。您可以在GET请求中发送它们,然后让_method来完成它(它将在url中,而不是在正文中),或者在ajax调用中使用DELETE方法

// var $tr = $(this).closest('tr');
$.ajax(
        {
            url: "/dashboard/booking/deletebooking/"+id,
            dataType: "JSON",
            type: 'DELETE',
            data: {
                '_token': $('meta[name=csrf-token]').attr("content"),
            },
            success: function ()
            {
                console.log("it Work");
            }
        });

答案 1 :(得分:0)

因为我认为您有类似的错误

  

方法Illuminate \ Database \ Eloquent \ Collection :: delete不存在。

反而尝试这样的事情

a = {'fruits': ['apple', 'orange'], 'tv': ['mi', 'lg']}
b = 'apple'

print([k for k,v in a.items() if b in v]) 

以便$booking = Booking::where('id', '=', $id)->first(); $booking->delete(); 可以使用方法$booking