首页 > 解决方案 > Laravel Eloquent 更新失败,但没有得到任何异常

问题描述

            $encodedArray = json_encode($existingArray);

            try {
                $bandwidthUpdate = ClientBandwidth::where(['client_id' => $clientId, 'date' => $dateParam]);

                if($bandwidthUpdate->update(['bandwidth' => $encodedArray]))
                {
                    Log::info('bandwidth updated !! '.$dateParam.':::'.'bandwidth type '.$bandwidthType .' Client '.$clientId);
                }else
                {
                    Log::error('bandwidth failed !! '.$dateParam.':::'.'bandwidth type '.$bandwidthType .' Client '.$clientId. ' Refernce '. $refernceKey);
                }

            }
            catch (QueryException $e) {
                Log::error('Exception !! '.$e->getMessage());
                Log::error('Exception !! '.$e->getTraceAsString());
                Log::error('bandwidth update failed !! '.$dateParam.':::'.'bandwidth type '.$bandwidthType .' Client '.$clientId);
            }
            catch (Exception $ex)
            {
                Log::error('Exception !! '.$ex->getMessage());
                Log::error('Exception !! '.$ex->getTraceAsString());
                Log::error('bandwidth failed !! '.$dateParam.':::'.'bandwidth type '.$bandwidthType .' Client '.$clientId);
            }

经常 $bandwidthUpdate->update(['bandwidth' => $encodedArray]) 返回 false。但我不知道为什么会这样。例外对我不起作用。任何人都知道如何获得未更新的确切原因。

这是数据库结构

CREATE TABLE `client_bandwidth` (
  `id` varchar(64) NOT NULL,
  `client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `bandwidth` text DEFAULT NULL,
  `total_bandwidth` bigint(20) NOT NULL DEFAULT 0,
  `date` date DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
   PRIMARY KEY (id)
);

这是示例 $existingArray,然后它解析为将值发送到数据库的 json_encode。

{
    "static": {
        "type": "static",
        "data": {
            "0130": {
                "hour": "01:30",
                "bandwidth": "1304802"
            },
            "0215": {
                "hour": "02:15",
                "bandwidth": "1794285"
            },
            "0245": {
                "hour": "02:45",
                "bandwidth": "534156"
            },
            "0300": {
                "hour": "03:00",
                "bandwidth": "4105499"
            },
            "0315": {
                "hour": "03:15",
                "bandwidth": "107027"
            },
            "0330": {
                "hour": "03:30",
                "bandwidth": "1487364"
            },
            "0430": {
                "hour": "04:30",
                "bandwidth": "417"
            },
            "0445": {
                "hour": "04:45",
                "bandwidth": "3620927"
            },
            "0500": {
                "hour": "05:00",
                "bandwidth": "8820971"
            },
            "0515": {
                "hour": "05:15",
                "bandwidth": "40111"
            },
            "0530": {
                "hour": "05:30",
                "bandwidth": "1759288"
            },
            "0545": {
                "hour": "05:45",
                "bandwidth": "883860"
            },
            "0615": {
                "hour": "06:15",
                "bandwidth": "48320"
            },
            "0630": {
                "hour": "06:30",
                "bandwidth": "6324949"
            },
            "0645": {
                "hour": "06:45",
                "bandwidth": "3543983"
            },
            "0715": {
                "hour": "07:15",
                "bandwidth": "7186102"
            },
            "0730": {
                "hour": "07:30",
                "bandwidth": "572259"
            },
            "0800": {
                "hour": "08:00",
                "bandwidth": "1623099"
            },
            "0815": {
                "hour": "08:15",
                "bandwidth": "2898736"
            },
            "0830": {
                "hour": "08:30",
                "bandwidth": "1468575"
            },
            "0845": {
                "hour": "08:45",
                "bandwidth": "17884836"
            },
            "0900": {
                "hour": "09:00",
                "bandwidth": "14234340"
            },
            "0915": {
                "hour": "09:15",
                "bandwidth": "10235920"
            },
            "0930": {
                "hour": "09:30",
                "bandwidth": "89926519"
            },
            "0945": {
                "hour": "09:45",
                "bandwidth": "1352073"
            },
            "1000": {
                "hour": "10:00",
                "bandwidth": "901410"
            },
            "1030": {
                "hour": "10:30",
                "bandwidth": "55466"
            },
            "1045": {
                "hour": "10:45",
                "bandwidth": "2431095"
            },
            "1115": {
                "hour": "11:15",
                "bandwidth": "417"
            },
            "1130": {
                "hour": "11:30",
                "bandwidth": "1725818"
            },
            "1145": {
                "hour": "11:45",
                "bandwidth": "1242887"
            },
            "1300": {
                "hour": "13:00",
                "bandwidth": "34994"
            },
            "1330": {
                "hour": "13:30",
                "bandwidth": "2914821"
            },
            "1345": {
                "hour": "13:45",
                "bandwidth": "1721874"
            },
            "1415": {
                "hour": "14:15",
                "bandwidth": "1587430"
            },
            "1430": {
                "hour": "14:30",
                "bandwidth": "340454"
            },
            "1630": {
                "hour": "16:30",
                "bandwidth": "48017"
            },
            "1700": {
                "hour": "17:00",
                "bandwidth": "40867"
            },
            "1730": {
                "hour": "17:30",
                "bandwidth": "118178"
            },
            "1800": {
                "hour": "18:00",
                "bandwidth": "1328435"
            },
            "1815": {
                "hour": "18:15",
                "bandwidth": "2587639"
            },
            "1845": {
                "hour": "18:45",
                "bandwidth": "141667"
            },
            "2200": {
                "hour": "22:00",
                "bandwidth": "72499"
            }
        },
        "total_bandwidth": 199052386
    },
    "user_agent": {
        "type": "user_agent",
        "data": {
            "0715": {
                "hour": "07:15",
                "bandwidth": "1737"
            },
            "0845": {
                "hour": "08:45",
                "bandwidth": "3230"
            }
        },
        "total_bandwidth": 4967
    }

}

标签: exceptioneloquentlaravel-5.8

解决方案


批量更新查询返回受查询影响的行数,不是 true/false 表示成功。

$bandwidthUpdate->update(['bandwidth' => $encodedArray])可以0在两种情况下返回:

  1. $bandwidthUpdate查询返回 0 行
  2. 查询返回行但未更改值

因此,如果您多次使用相同的代码运行代码$encodedArray- 第二点可能会解释为什么您0每次都会得到。


推荐阅读