Laravel method delete Ajax Request

Posted 7 months ago by palla451

HTML

                        <table id="table_id" class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th>id</th>
                                <th>Room Id</th>
                                <th>Location</th>
                                <th>Start</th>
                                <th>End</th>
                                <th>Handles</th>
                            </tr>
                            </thead>
                            <tbody>
                                @foreach($bookings as $booking)
                                    <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>
                                            <button type="submit" class="deleteProduct" data-id="{{ $booking->id }}" data-token="{{ csrf_token() }}">Delete</button>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>

Javascript:

$(".deleteProduct").click(function(event){
    var id = $(this).data('id');
    var token = $(this).data('token');
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax(
            {
                url: '/dashboard/booking/deletebooking/'+id,
                type: 'delete', // replaced from put
                dataType: "JSON",
                data: {
                    "id": id // method and token not needed in data
                },
                success: function (response)
                {
                    console.log(response); // see the reponse sent
                },
                error: function(xhr) {
                    console.log(xhr.responseText); // this line will save you tons of hours while debugging
                    // do something here because of error
                }
            });
});

Route:

Route::delete('dashboard/booking/deletebooking/{id}','[email protected]')->name('works.deletebooking');

ResourceController

public function deletebooking($id){

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

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

}

I have the error: message Method Illuminate\Database\Eloquent\Collection::delete does not exist. exception BadMethodCallException file /home/vagrant/code/pickbooking/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php line 100

Where am I wrong, please

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.