【发布时间】:2019-03-23 22:57:10
【问题描述】:
Laravel 中的模型
“此集合实例上不存在属性 [task_category]。”
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::get();
$new_spent_time->task_category = (['{task_category}' => $new_spent_time->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);
$request['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);
$request['percentage'] = (int)$new_spent_time->percentage + $spent_time->daily_percentage;
return $spent_time->update($data);
}
}
【问题讨论】:
-
您的
spenttime表迁移中有task_category字段吗? -
@thisiskelvin 是的,当然
-
您的
SpentTime::get()函数的用途是什么?是为了获得一审吗?创建一个新的?如果要获得第一个,请使用::first()。 -
@thisiskelvin 谢谢,但还有一个问题,保存创建新数据时,无法计算具有相同类别的数据,我寻求帮助。拜托,你可以看到链接stackoverflow.com/questions/52865862/…
标签: php laravel sum laravel-query-builder laravel-5.7