Files
rootandClaude Sonnet 5 e74aa2bda4 Add a community workout library
Workouts were private to their owner with no way to browse or copy
another user's - FavoriteController::toggleWorkout even had a comment
anticipating sharing ("users can still favorite others' workouts if
shared later") that was never followed up on.

Adds an is_public flag workouts can be toggled to from their own page,
a /workouts/community browse+search page, and a copy action that
duplicates a public workout (including all its exercises/sets/reps)
into the copier's own library.

Making this work required splitting workout authorization into two
tiers: authorizeView() (owner, the assigning trainer, or anyone if the
workout is public - read-only) and the existing authorizeWorkout()
(owner or assigning trainer - can add/edit/remove exercises). Deletion
stays strictly owner-only as before. The show page now hides the
"Add Exercise" panel and per-exercise edit/remove controls for viewers
who can see but not manage a workout.

Found and fixed a real Blade compiler quirk via live testing: `@if`
directly after a word character with no space (`exercises@if(...)`)
doesn't compile - Blade's directive regex treats it as email-like text
and leaves it literal - while the matching `@endif` still compiles,
producing invalid mismatched PHP. Fixed by adding a space.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-10 12:39:16 +03:00

67 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Workout extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'title',
'notes',
'estimated_minutes',
'program_id',
'week_number',
'day_number',
'assigned_by_trainer_id',
'is_public',
];
protected $casts = [
'estimated_minutes' => 'integer',
'week_number' => 'integer',
'day_number' => 'integer',
'is_public' => 'boolean',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function program(): BelongsTo
{
return $this->belongsTo(Program::class);
}
public function assignedByTrainer(): BelongsTo
{
return $this->belongsTo(User::class, 'assigned_by_trainer_id');
}
public function items(): HasMany
{
return $this->hasMany(WorkoutExercise::class)->orderBy('position');
}
public function exercises(): BelongsToMany
{
return $this->belongsToMany(Exercise::class, 'workout_exercises')
->withPivot(['id', 'position', 'sets', 'reps', 'weight', 'rest_seconds', 'notes'])
->withTimestamps()
->orderBy('workout_exercises.position');
}
public function favoritedBy()
{
return $this->belongsToMany(User::class, 'workout_favorites')->withTimestamps();
}
}