public static function findOrCreate($plan_id, $data)
{
$fromDate = Carbon::now()->subDay()->startOfWeek();
$nowDate = Carbon::now()->
the data is there, when dd($spent_time);
dd($spent_time);
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);
}
}
In your SpentTime
model, you are able to create accessors which are functions that can be used here to query the sum a day of all relating records:
public function getDailySpentTimeAttribute()
{
return self::where('task_category_id', $this->task_category_id)
->get()
->sum('daily_spent_time');
}
public function getDailyPercentageAttribute()
{
return self::where('task_category_id', $this->task_category_id)
->get()
->sum('daily_percentage');
}
Here, we create two accessors
, one to get the daily spent time and another to get the daily percentage, for all records based on relating task_category
.
There can be called using the following:
$dailySpentTime = SpentTime::find($id)->dailySpentTime;
// or within your blade template
{{ $spentTime->dailySpentTime }}
Within your controller, as you no longer have to run any calculations upon saving, you can do the following:
public function store(Request $request)
{
$spent_time = SpentTime::findOrCreate($request->get('plan_id'), [
'task_category' => $request->get('task_category'),
'reason' => $request->get('reason'),
]);
return redirect()->route('real.index', compact('spent_time'));
}
Make sure to delete your custom findOrCreate()
method which is currently overriding the laravel version.
Hopefully this helps.
public static function findOrCreate($plan_id, $data)
{
$spent_time = static::where('plan_id', $plan_id)->first();
$task_category = $spent_time->task_category;
if (is_null($spent_time)) {
return static::create($data);
}else{
$spent_time['spent_time'] = $spent_time->spent_time + $spent_time->daily_spent_time;
$spent_time['percentage'] = $spent_time->percentage + $spent_time->daily_percentage;
return $spent_time->update($data);
}
}
public function index()
{
$spent_times = SpentTime::orderBy('task_category')->where('created_at', '>=', Carbon::today()->toDateString())->paginate(10);
$user_stories = Plan::pluck('user_story', 'id');
$real = new SpentTime;
return view('reals.index', compact('spent_times', 'user_stories', 'real'));
}
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'));
}