【发布时间】:2019-03-24 04:13:27
【问题描述】:
型号
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::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->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'));
}
查看
有问题,保存创建新数据时出现错误“尝试获取非对象的属性”
但是有一个数据在创建新数据的时候可以保存,但是还不能计算数据,而其他类不能这样,反而报错
应该从这个问题中纠正什么?
【问题讨论】:
-
$new_spent_time根本没有被保存。您还在创建更新properties和数组keys。选择一个。 -
那么应该改进什么?对不起,我还在学习,所以还是不明白你的意思。
-
结构有点难以理解,我看了看,但对你在哪里使用
where()子句感到困惑。您可以在一条记录上运行where()。 -
那么要改变什么?
-
您能否用您的模型和模型关系更新您的问题,以便我了解要实现的目标。
标签: php laravel sum laravel-query-builder laravel-5.7