can save when create new data, but can't calculated data by category

旧时模样 提交于 2020-01-03 02:37:25

问题


Model

can save when create new data, but can't calculated data by 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::first();
        $task_category = $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'] = $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'] = $new_spent_time->percentage  + $spent_time->daily_percentage;

        $new_spent_time->save();
        return $spent_time->update($data);
    }
}

can't calculated data by category

来源:https://stackoverflow.com/questions/52924260/can-save-when-create-new-data-but-cant-calculated-data-by-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!