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>
108 lines
2.6 KiB
PHP
108 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
|
use HasFactory, Notifiable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'age',
|
|
'weight_kg',
|
|
'height_cm',
|
|
'fitness_level',
|
|
'goal',
|
|
'health_notes',
|
|
'available_equipment',
|
|
'days_per_week',
|
|
'minutes_per_session',
|
|
'is_trainer',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'age' => 'integer',
|
|
'weight_kg' => 'decimal:2',
|
|
'height_cm' => 'decimal:2',
|
|
'days_per_week' => 'integer',
|
|
'minutes_per_session' => 'integer',
|
|
'is_admin' => 'boolean',
|
|
'is_trainer' => 'boolean',
|
|
];
|
|
}
|
|
|
|
// Relationships
|
|
public function workouts()
|
|
{
|
|
return $this->hasMany(Workout::class);
|
|
}
|
|
|
|
public function favoriteExercises()
|
|
{
|
|
return $this->belongsToMany(Exercise::class, 'exercise_favorites')->withTimestamps();
|
|
}
|
|
|
|
public function favoriteWorkouts()
|
|
{
|
|
return $this->belongsToMany(Workout::class, 'workout_favorites')->withTimestamps();
|
|
}
|
|
|
|
public function favoriteYogaPoses()
|
|
{
|
|
return $this->belongsToMany(YogaPose::class, 'yoga_pose_favorites')->withTimestamps();
|
|
}
|
|
|
|
public function weightLogs()
|
|
{
|
|
return $this->hasMany(WeightLog::class);
|
|
}
|
|
|
|
// Helpers
|
|
public function hasFavoritedExercise(Exercise $exercise): bool
|
|
{
|
|
return $this->favoriteExercises()->where('exercise_id', $exercise->id)->exists();
|
|
}
|
|
|
|
public function hasFavoritedWorkout(Workout $workout): bool
|
|
{
|
|
return $this->favoriteWorkouts()->where('workout_id', $workout->id)->exists();
|
|
}
|
|
|
|
public function hasFavoritedYogaPose(YogaPose $pose): bool
|
|
{
|
|
return $this->favoriteYogaPoses()->where('yoga_pose_id', $pose->id)->exists();
|
|
}
|
|
}
|