Laravel Eloquent 更新失败,但没有得到任何异常

Posted

技术标签:

【中文标题】Laravel Eloquent 更新失败,但没有得到任何异常【英文标题】:Laravel Eloquent Update failed , but didn't get any exception 【发布时间】:2021-01-13 02:12:36 【问题描述】:
            $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
    


【问题讨论】:

【参考方案1】:

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

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

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

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

【讨论】:

以上是关于Laravel Eloquent 更新失败,但没有得到任何异常的主要内容,如果未能解决你的问题,请参考以下文章

使用 Laravel Eloquent 使用复合键更新表

Laravel Eloquent - 更新()函数

Laravel Eloquent 方法 updateOrCreate 更新时超过执行时间

有没有办法在 Laravel 5.8 中测试 Eloquent Observers?

Laravel 测试身份验证。使用 Illuminate\Database\Eloquent\ModelNotFoundException 的后续登录尝试失败

没有 laravel 的 Eloquent 选择行不起作用