热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php在Laravel中创建新数据时无法按类别保存计算数据

模型花费时间在Laravel中创建新数据时无法按类别保存计算数据publicstaticfunctionfindOrCreate($plan_id,$data){$fromDate

模型花费时间

在Laravel中创建新数据时无法按类别保存计算数据

public static function findOrCreate($plan_id, $data)
{
$fromDate = Carbon::now()->subDay()->startOfWeek()->toDateString();
$nowDate = Carbon::now()->today()->toDateString();
$spent_time = static::where('plan_id', $plan_id)->first();
if (is_null($spent_time)) {
return static::create($data);
}else{
$task_category = SpentTime::where('task_category', $spent_time->task_category)->get();
$create_spent_times = SpentTime::where('task_category', $spent_time->task_category)->sum('daily_spent_time', $fromDate);
$request['spent_time'] = (int)$create_spent_times + $spent_time->daily_spent_time;
$create_spent_times = SpentTime::where('task_category', $spent_time->task_category)->sum('daily_percentage', $fromDate);
$request['percentage'] = (int)$create_spent_times + $spent_time->daily_percentage;
$spent_time->save();
return $spent_time->update($data);
}
}

TrackerController

这是存储数据的控制器功能:

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'));
}

解决方法:

更新答案


can create new data, but can not calculate data by category


enter image description here

模型

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;
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'));
}


推荐阅读
author-avatar
小丁啊小丁
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有