Laravel method delete Ajax Request

Posted 2 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.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.