diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index bff0597..e0db472 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -29,12 +29,39 @@ class JournalEntryController extends Controller public function index(Request $request): View { $date = $request->date ?? Carbon::now()->toDateString(); + $date = Carbon::rawCreateFromFormat('Y-m-d', $date); + + // Get entries and nutrient sums for the day. + $entries = JournalEntry::where([ + 'user_id' => Auth::user()->id, + 'date' => $date->toDateString(), + ])->get(); + $sums = []; + foreach (Nutrients::$all as $nutrient) { + $sums[$nutrient['value']] = round($entries->sum($nutrient['value'])); + } + + // Get daily goals data for user. + $goals = Auth::user()->getGoalsByTime($date); + $dailyGoals = []; + foreach (Nutrients::$all as $nutrient) { + $goal = $goals['present'] + ->where('frequency', 'daily') + ->where('name', $nutrient['value']) + ->first(); + if ($goal) { + $dailyGoals[$goal->name] = round($sums[$goal->name] / $goal->goal * 100); + if ($dailyGoals[$goal->name] > 0) { + $dailyGoals[$goal->name] .= '%'; + } + } + } + return view('journal-entries.index') - ->with('entries', JournalEntry::where([ - 'user_id' => Auth::user()->id, - 'date' => $date, - ])->get()) - ->with('date', Carbon::createFromFormat('Y-m-d', $date)); + ->with('entries', $entries) + ->with('sums', $sums) + ->with('dailyGoals', $dailyGoals) + ->with('date', $date); } /** diff --git a/resources/views/journal-entries/index.blade.php b/resources/views/journal-entries/index.blade.php index b4a1a46..d4c419e 100644 --- a/resources/views/journal-entries/index.blade.php +++ b/resources/views/journal-entries/index.blade.php @@ -37,22 +37,53 @@
-
+

{{ $date->format('D, j M Y') }}

{{ $entries->count() }} {{ \Illuminate\Support\Pluralizer::plural('entry', $entries->count()) }}
-
-
Calories
-
{{ round($entries->sum('calories'), 2) }}
-
Fat
-
{{ round($entries->sum('fat'), 2) }}g
-
Cholesterol
-
{{ round($entries->sum('cholesterol'), 2) }}mg
-
Sodium
-
{{ round($entries->sum('sodium'), 2) }}mg
-
Carbohydrates
-
{{ round($entries->sum('carbohydrates'), 2) }}g
-
Protein
-
{{ round($entries->sum('protein'), 2) }}g
+
% Daily goal
+
+
+ Calories + {{ number_format($sums['calories']) }} +
+
+ {{ $dailyGoals['calories'] ?? 'N/A' }} +
+
+ Fat + {{ number_format($sums['fat']) }}g +
+
+ {{ $dailyGoals['fat'] ?? 'N/A' }} +
+
+ Cholesterol + {{ number_format($sums['cholesterol']) }}mg +
+
+ {{ $dailyGoals['cholesterol'] ?? 'N/A' }} +
+
+ Sodium + {{ number_format($sums['sodium']) }}mg +
+
+ {{ $dailyGoals['sodium'] ?? 'N/A' }} +
+
+ Carbohydrates + {{ number_format($sums['carbohydrates']) }}g +
+
+ {{ $dailyGoals['carbohydrates'] ?? 'N/A' }} +
+
+ Protein + {{ number_format($sums['protein']) }}g +
+
+ {{ $dailyGoals['protein'] ?? 'N/A' }} +