user()->id); // Get all pomos and calculate the average time spent per todo (due_end - due_start)/count/total pomos $pomos = $user->projects->map(function ($project) { return $project->todos->map(function ($todo) { return $todo->pomo; }); })->flatten(); $total_pomos = $pomos->count(); if ($total_pomos === 0) { $this->ave_pomo_time = 0; return; } $total_time = 0; foreach ($pomos as $pomo) { $total_time += $pomo->pomo_end - $pomo->pomo_start; } $this->ave_pomo_time = $total_time / $total_pomos; // Time in Hours and Minutes (H hours m minutes) $this->ave_pomo_time = Carbon::createFromTimestamp($this->ave_pomo_time)->format('H \h m \m'); } public function render() { return view('livewire.dashboard.pomo-time', [ 'ave_pomo_time' => $this->ave_pomo_time, ]); } }