Add body weight logging with a trend on the dashboard
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>
This commit is contained in:
@@ -88,5 +88,51 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-8 rounded-2xl ring-1 ring-gray-200 dark:ring-gray-700 bg-white dark:bg-gray-800 p-5">
|
||||
<div class="flex items-center justify-between flex-wrap gap-3">
|
||||
<div>
|
||||
<h2 class="text-lg font-semibold text-gray-900 dark:text-white">{{ __('app.weight_log.title') }}</h2>
|
||||
@if($weightChange !== null)
|
||||
<p class="mt-1 text-sm text-gray-600 dark:text-gray-300">
|
||||
{{ __('app.weight_log.change_since_first') }}
|
||||
<span class="font-semibold {{ $weightChange < 0 ? 'text-emerald-600' : ($weightChange > 0 ? 'text-amber-600' : 'text-gray-600') }}">
|
||||
{{ $weightChange > 0 ? '+' : '' }}{{ $weightChange }} kg
|
||||
</span>
|
||||
</p>
|
||||
@endif
|
||||
</div>
|
||||
<form method="POST" action="{{ route('weight-logs.store') }}" class="flex items-end gap-2">
|
||||
@csrf
|
||||
<div>
|
||||
<label class="block text-xs font-medium text-gray-500 dark:text-gray-400">{{ __('app.weight_log.weight_kg') }}</label>
|
||||
<input type="number" step="0.1" min="20" max="400" name="weight_kg" required class="mt-1 w-28 rounded-lg border-gray-300 dark:border-gray-700 bg-white/80 dark:bg-gray-900 text-gray-900 dark:text-gray-100 px-3 py-2 text-sm">
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-xs font-medium text-gray-500 dark:text-gray-400">{{ __('app.weight_log.date') }}</label>
|
||||
<input type="date" name="logged_at" max="{{ now()->toDateString() }}" value="{{ now()->toDateString() }}" class="mt-1 rounded-lg border-gray-300 dark:border-gray-700 bg-white/80 dark:bg-gray-900 text-gray-900 dark:text-gray-100 px-3 py-2 text-sm">
|
||||
</div>
|
||||
<button type="submit" class="inline-flex items-center rounded-lg bg-indigo-600 px-3 py-2 text-sm font-semibold text-white hover:bg-indigo-500">{{ __('app.weight_log.log') }}</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@if($weightLogs->isEmpty())
|
||||
<div class="mt-4 rounded-xl border border-dashed border-gray-300 dark:border-gray-700 p-6 text-sm text-gray-600 dark:text-gray-300">{{ __('app.weight_log.none') }}</div>
|
||||
@else
|
||||
<div class="mt-4 flex flex-wrap gap-2">
|
||||
@foreach($weightLogs->reverse()->take(10) as $log)
|
||||
<div class="inline-flex items-center gap-2 rounded-full bg-gray-100 dark:bg-gray-900 ring-1 ring-gray-200 dark:ring-gray-700 pl-3 pr-1.5 py-1 text-sm">
|
||||
<span class="font-semibold text-gray-900 dark:text-white">{{ rtrim(rtrim(number_format($log->weight_kg, 1), '0'), '.') }} kg</span>
|
||||
<span class="text-gray-500">{{ $log->logged_at->format('M j') }}</span>
|
||||
<form method="POST" action="{{ route('weight-logs.destroy', $log) }}" onsubmit="return confirm('{{ __('app.weight_log.confirm_delete') }}');">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="text-gray-400 hover:text-red-500 rounded-full h-5 w-5 inline-flex items-center justify-center" aria-label="{{ __('app.weight_log.delete') }}">×</button>
|
||||
</form>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
</section>
|
||||
</x-app-layout>
|
||||
|
||||
Reference in New Issue
Block a user