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.