Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

palla451's avatar

Laravel method delete Ajax Request

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}','ResourceController@deletebooking')->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

0 likes
6 replies
arthvrian's avatar

And the error is?

in the AJAX function? did you see the console? in the response? did you check if the method receive the id, the id exists, etc?

After your edit :P

You have a colection (the ->get() part) and try to delete it

try

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

// OR

$booking = Booking::find($id);
$booking->delete();
1 like
lanatel's avatar

instead of Booking::where('id','=',$id)->get(); use Booking::where('id','=',$id)->first();

D9705996's avatar

This is a perfect use case for route model binding

public function deletebooking(Booking $booking) {

    $booking->delete();

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

}

Then change your route parameter to booking instead of I'd e.g. Route::delete('booking/{booking}')

The benefits are cleaner code and it will automatically handle when provided a non existent id

https://laravel.com/docs/5.7/routing#route-model-binding

palla451's avatar

First of all thank you all for the answers. I solved the problem. My query is as in this example (join with two table:

$bookings = DB::table('bookings')->where('info','like','PICKCENTER%') ->join('rooms','bookings.room_id','=','rooms.id') ->join('locations','bookings.location_id','=','locations.id') ->get(['bookings.id','bookings.room_id', 'bookings.location_id','locations.sede','bookings.start_date','bookings.end_date','rooms.name']);

then my function delete is this :

public function deletebooking($id){

    DB::table('bookings')->where('id',$id)->delete();

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

Add some validation, in order that a user cannot delete all your bookings :)

palla451's avatar

@VILFAGO - This function is able only for super admin.

for web.php i have

Route::prefix('dashboard')->middleware(['auth'])

after i have create permission:

public function __construct() { $this->middleware('permission:create-room|read-room|update-room|delete-room');

    $this->data = [
        'pageTitle' => __('Room Works'),
        'pageHeader' => __('Room Works'),
        'pageSubHeader' => __('Booking Rooms for works')
    ];
}

i have user, managment , admin and superadmin

Please or to participate in this conversation.