【发布时间】:2019-03-22 21:49:06
【问题描述】:
laravel 中的模型
"数组到字符串的转换(SQL:更新
spent_times设置updated_at=2018-10-18 06:02:29,spent_time=12,percentage=60.00,task_category=测试哪里@ 987654328@ = 7) ◀"
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{
$new_spent_time = SpentTime::find($plan_id);
$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->spent_time = $new_spent_time::where('task_category', $task_category)->sum('daily_spent_time', $new_spent_time->daily_spent_time, $fromDate);
$request['spent_time'] = (int)$new_spent_time->spent_time + $spent_time->daily_spent_time;
$new_spent_time->percentage = SpentTime::where('task_category', $spent_time->task_category)->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);
$request['percentage'] = (int)$new_spent_time->percentage + $spent_time->daily_percentage;
$new_spent_time->save();
return $spent_time->update($data);
}
}
laravel 中的控制器
在函数库中,新建数据时不能保存数据,也不能按类别计算数据,需要计算数据并按图片输入表格。
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'));
}
【问题讨论】:
-
您创建 $new_raspent_time->task_category 作为数组并将其用作字符串
-
@ImranQamer 那么我应该使用什么? :) 寻求帮助
-
可以给我看看你的表格数据吗?
-
同时发布带有行号的完整错误消息和错误的完整查询。
-
@ImranQamer 在表花费时间:id、花费时间、daily_spent_time、百分比、daily_percentage、task_category、reason 和 plan_id 表计划:id、assign、estimate_time、user_story、lateness、index 和 project_id
标签: php laravel-5 sum laravel-query-builder