我有一个if语句,用于检查包含分配给用户的'services'数组的请求,然后在它们具有'checked'值时删除/添加它们。
我目前遇到的问题是,即使使用 - >首先指定一行并且具体指定值,它也会删除数据库中的所有行。
// Loop through all the services
foreach ($request->services as $key => $value) {
$serviceRecord = ServiceUser::where('user_id', $userId)->where('id', $value['id'])->first();
// The service is checked and it doesn't currently exist in the pivot table (not assigned to user)
if ($value['checked'] && !$serviceRecord) {
// Create an array of new values to be inserted
// The service is not checked and does exist in the user_service table (currently assigned, wanting to be removed)
} elseif (!$value['checked'] && $serviceRecord) {
$serviceId = $serviceRecord->id;
// $activeBookingsWithService is a count to see if the service can be deleted
if (!count($activeBookingsWithService)) {
$serviceRecord->delete();
}
}
}
这是if语句的略微简化版本。在尝试调试时,我可以确认:
是否有任何明显可以解释为什么所有行都被删除?我还更换了删除部分并更新了行值,这也更新了数据库中的所有记录。