drfls28608 2017-09-27 11:24
浏览 758

Laravel删除方法删除所有行

I have an if statement that checks a request that contains an array of 'services' that are assigned to a user and then removes / adds them if they have a 'checked' value.

The issue I am currently having is that even when specifying a row using ->first and specific where values, it is deleting all of the rows in the database.

// 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();
        }
    }
}

This is a slightly stripped down version of the if statement. When trying to debug I can confirm:

  • $request->services only contains one item
  • $userId is the correct value
  • $serviceRecord only returns 1 value that is correct

Is there anything obvious that might explain why all rows are being deleted? I have also swapped out the deletion part and updated the row values instead which also updates all the records in the database.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算