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

Freddie19's avatar

Data 'status' not updated

I have a code that doesn't work, so I want to update one of the columns, namely 'status' by looping because it will update the data, maybe not only 1, I also happen to have the same 2 status columns, namely in bookings, and in tenants, but I I only want to retrieve status data on bookings, how do I fix this?

$overdue = Booking::leftJoin('tenants', 'tenants.id', 'bookings.tenant_id')
            ->leftJoin('tenant_details', 'tenant_details.tenant_id', 'tenants.id')
            ->leftJoin('users', 'users.id', 'tenant_details.user_id')
            ->where('bookings.status', 'pending')
            ->where('start_time', '<', now())
            ->where('bookings.status', '!=', 'cancelled');

        $overdueBookings = $overdue->select('bookings.status', 'users.email', 'bookings.start_time', 'bookings.end_time')->get()->filter(function ($booking) {
            // return $booking;
            return $booking->status === 'pending' && now() > $booking->start_time && $booking->status !== 'cancelled';
        });


        $overdueBookings->each(function ($booking) {
            $booking->update([
                'status' => 'cancelled',
            ]);
        });
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To update only the 'status' column in the 'bookings' table, you can modify the update query in the each loop to only update the 'status' column. Here's an updated version of the code:

$overdue = Booking::leftJoin('tenants', 'tenants.id', 'bookings.tenant_id')
            ->leftJoin('tenant_details', 'tenant_details.tenant_id', 'tenants.id')
            ->leftJoin('users', 'users.id', 'tenant_details.user_id')
            ->where('bookings.status', 'pending')
            ->where('start_time', '<', now())
            ->where('bookings.status', '!=', 'cancelled');

$overdueBookings = $overdue->select('bookings.id', 'bookings.status', 'users.email', 'bookings.start_time', 'bookings.end_time')->get()->filter(function ($booking) {
    return $booking->status === 'pending' && now() > $booking->start_time && $booking->status !== 'cancelled';
});

$overdueBookings->each(function ($booking) {
    Booking::where('id', $booking->id)->update([
        'status' => 'cancelled',
    ]);
});

In the each loop, we're now using the Booking model to update the 'status' column in the 'bookings' table, instead of updating the entire row. We're also selecting the 'id' column in the query to make sure we're only updating the correct row.

Please or to participate in this conversation.