Cannot save calculation data by category when create new data in laravel

前端 未结 2 755
小鲜肉
小鲜肉 2021-01-16 19:52

Model SpentTime

I am unable save calculation data by category when create new data in laravel

public static function findOrCreate($plan_id, $data)
         


        
2条回答
  •  野的像风
    2021-01-16 20:35

    Update Answer

    can create new data, but can not calculate data by category

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

    Controller

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

提交回复
热议问题