mercredi 17 octobre 2018

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

Model SpentTime

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

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

    $spent_time = static::where('plan_id', $plan_id)->first();

    if (is_null($spent_time)) {
        return static::create($data);
    }else{
        $task_category = SpentTime::where('task_category', $spent_time->task_category)->get();
        $create_spent_times = SpentTime::where('task_category', $spent_time->task_category)->sum('daily_spent_time', $fromDate);
        $request['spent_time'] = (int)$create_spent_times + $spent_time->daily_spent_time;

        $create_spent_times = SpentTime::where('task_category', $spent_time->task_category)->sum('daily_percentage', $fromDate);
        $request['percentage'] = (int)$create_spent_times + $spent_time->daily_percentage;

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

TrackerController

this is function store in 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'));
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire