No way existed to log body weight over time - only the single
self-reported value from the signup profile - and the dashboard showed
session activity but nothing about physical progress, which is usually
the main reason people keep using a fitness app.
Adds a WeightLog model (one entry per user per day, re-logging the same
day updates it rather than duplicating), a quick-add form and trend
summary on the dashboard, and delete support.
Found and fixed a real bug along the way: the first implementation used
WeightLog::updateOrCreate() with a raw ['logged_at' => $date] match
array, which silently failed to find the same-day entry because the
`date` cast stores full datetime strings ('2026-01-01 00:00:00') while
the match array holds a plain 'Y-m-d' string - so re-logging the same
day hit a unique-constraint violation instead of updating. Fixed by
using a whereDate() lookup instead, which is comparison-format-safe.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
70 lines
2.4 KiB
PHP
70 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\WorkoutSession;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\View\View;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index(): View
|
|
{
|
|
$user = Auth::user();
|
|
|
|
// Basic counts
|
|
$workoutsCount = $user->workouts()->count();
|
|
$sessionsQuery = WorkoutSession::where('user_id', $user->id);
|
|
$sessionsCount = (clone $sessionsQuery)->count();
|
|
$completedSessions = (clone $sessionsQuery)->whereNotNull('completed_at')->count();
|
|
$completionRate = $sessionsCount > 0 ? round(($completedSessions / $sessionsCount) * 100) : 0;
|
|
|
|
// Recent sessions with progress
|
|
$recentSessions = (clone $sessionsQuery)
|
|
->latest('started_at')
|
|
->latest('id')
|
|
->with(['workout:id,title', 'items:id,session_id,done'])
|
|
->limit(5)
|
|
->get()
|
|
->map(function ($s) {
|
|
$total = $s->items->count();
|
|
$done = $s->items->where('done', true)->count();
|
|
$pct = $total > 0 ? (int) round(($done / $total) * 100) : 0;
|
|
return [
|
|
'id' => $s->id,
|
|
'workout_title' => optional($s->workout)->title ?? 'Workout',
|
|
'started_at' => $s->started_at,
|
|
'completed_at' => $s->completed_at,
|
|
'done' => $done,
|
|
'total' => $total,
|
|
'pct' => $pct,
|
|
];
|
|
});
|
|
|
|
// Activity in the last 30 days (number of sessions per day)
|
|
$since = now()->subDays(29)->startOfDay();
|
|
$dailyActivity = (clone $sessionsQuery)
|
|
->where('created_at', '>=', $since)
|
|
->get(['id', 'created_at'])
|
|
->groupBy(function ($s) { return $s->created_at->format('Y-m-d'); })
|
|
->map->count();
|
|
|
|
// Body weight trend
|
|
$weightLogs = $user->weightLogs()->orderBy('logged_at')->get(['id', 'weight_kg', 'logged_at']);
|
|
$weightChange = $weightLogs->count() >= 2
|
|
? round($weightLogs->last()->weight_kg - $weightLogs->first()->weight_kg, 2)
|
|
: null;
|
|
|
|
return view('dashboard', compact(
|
|
'workoutsCount',
|
|
'sessionsCount',
|
|
'completedSessions',
|
|
'completionRate',
|
|
'recentSessions',
|
|
'dailyActivity',
|
|
'weightLogs',
|
|
'weightChange'
|
|
));
|
|
}
|
|
}
|