php - 创建新数据时无法保存计算数据
问题描述
模型
public static function findOrCreate($plan_id, $data)
{
$fromDate = Carbon::now()->subDay()->startOfWeek();
$nowDate = Carbon::now()->today();
$spent_time = static::where('plan_id', $plan_id)->first();
if (is_null($spent_time)) {
return static::create($data);
}else{
$new_spent_time = SpentTime::first();
$task_category = $new_spent_time->task_category;
$new_spent_time->task_category = (['{task_category}' => $task_category,
'{daily_spent_time}' => $new_spent_time->daily_spent_time,
'{daily_percentage}' => $new_spent_time->daily_percentage,
'{spent_time}' => $new_spent_time->spent_time,
'{percentage}' => $new_spent_time->percentage, $new_spent_time->task_category]);
$new_spent_time->spent_time = $new_spent_time::where('task_category',$task_category)
->sum('daily_spent_time', $new_spent_time->daily_spent_time , $fromDate);
$new_spent_time['spent_time'] = (int)$new_spent_time->spent_time + $spent_time->daily_spent_time;
$new_spent_time->percentage = $new_spent_time::where('task_category',$task_category)
->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);
$new_spent_time['percentage'] = (int)$new_spent_time->percentage + $spent_time->daily_percentage;
$new_spent_time->save();
return $spent_time->update($data);
}
}
新建同类别数据库时,save无法计算数据
像这样的错误,创建新数据时无法保存计算数据
解决方案
推荐阅读
- html - 如何将一组列表元素居中并将它们并排放置?
- python - 如何获取熊猫中分类变量的计数?
- c# - System.ArgumentOutOfRangeException:年、月和日参数描述了无法表示的 c#
- javascript - 使localStorage无效并在组件挂载后发现它已更新
- javascript - 仅在移动设备中显示模式内的元素
- arraylist - 从 Prestodb 中的非结构化数组中获取字段
- xml-parsing - 读取大的xml文件
- asp.net-core - WebSockets Server .NET Core - IsWebSocketRequest 始终返回 false
- python - 有没有办法从 opencv 对象跟踪器 kcf 中提取 cv2.rectangle 的坐标?
- powershell - 多次提示退出菜单