how to calculate data by category when creating new data with the same category

后端 未结 4 1811
一生所求
一生所求 2021-01-17 01:02

Model

public static function findOrCreate($plan_id, $data)
{
    $fromDate = Carbon::now()->subDay()->startOfWeek();
    $nowDate = Carbon::now()->         


        
4条回答
  •  心在旅途
    2021-01-17 01:28

    update Model

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

提交回复
热议问题