public static function findOrCreate($plan_id, $data)
{
$fromDate = Carbon::now()->subDay()->startOfWeek()->toDateString();
$nowDate = Carbon::now()->today()->toDateString();
$spent_time = SpentTime::where('plan_id', $plan_id)->first();
$task_category = $spent_time->task_category;
if (is_null($spent_time)) {
return static::create($data);
}else{
$spent_time->spent_time = SpentTime::where('task_category',$task_category)
->sum('daily_spent_time', $spent_time->daily_spent_time , $fromDate);
$spent_time['spent_time'] = $spent_time->spent_time + $spent_time->daily_spent_time;
$spent_time->percentage = SpentTime::where('task_category',$task_category)
->sum('daily_percentage', $spent_time->daily_percentage, $fromDate);
$spent_time['percentage'] = $spent_time->percentage + $spent_time->daily_percentage;
return $spent_time->update($data);
}
}
在创建具有相同类别的新数据并输入数据值时,它应该能够正确计算数据,但这不能
计算与输入值不匹配
答案 0 :(得分:0)
public static function findOrCreate($plan_id, $data)
{
$spent_time = static::where('plan_id', $plan_id)->first();
$task_category = $spent_time->task_category;
if (is_null($spent_time)) {
return static::create($data);
}else{
$spent_time['spent_time'] = $spent_time->spent_time + $spent_time->daily_spent_time;
$spent_time['percentage'] = $spent_time->percentage + $spent_time->daily_percentage;
return $spent_time->update($data);
}
}
public function store(Request $request)
{
$spent_time = SpentTime::findOrCreate($request->get('plan_id'), [
'plan_id' => $request->get ('plan_id'),
'daily_spent_time' => $request->get ('daily_spent_time'),
'daily_percentage' => $request->get ('daily_percentage'),
'reason' => $request->get ('reason'),
]);
return redirect()->route('real.index', compact( 'spent_time'));
}