Laravel Ajax delete record with button

Viewed 2232

I do not understand why it does not work:

Route

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);
}

Table

<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>

Request 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");
});

I have this error:

Request URL: http://pickbooking.local/dashboard/booking/deletebooking/1 Request Method: POST Status Code: 500 Internal Server Error Remote Address: 192.168.10.10:80

2 Answers

The issue is in the method used for the ajax call 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");
            }
        });

the data will be sent in the body of the request, and in a DELETE request, there is no body. so laravel wont see the _method, or the _token. Either you send them in a GET request and let the _method do it's job (it will be in the url, not in the body), Or use the DELETE method in the ajax call

// 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");
            }
        });

Because I think you have an error something like

Method Illuminate\Database\Eloquent\Collection::delete does not exist.

Instead try something like this

$booking = Booking::where('id', '=', $id)->first();
$booking->delete();

so that $booking can have method delete()

Related