Files
fitness/app/Http/Controllers/FavoriteController.php
T
rootandClaude Sonnet 5 7769d2038a Add favorite toggle to yoga poses
Exercises and workouts both have a favorite toggle; yoga poses had an
otherwise identical browse/filter UX but no way to favorite them - an
inconsistency rather than a deliberate omission. Mirrors the existing
exercise/workout favorite pattern exactly: a pivot table, a
belongsToMany on User, and a toggle button on the pose detail page.

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

58 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Exercise;
use App\Models\Workout;
use App\Models\YogaPose;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
class FavoriteController extends Controller
{
public function toggleExercise(Exercise $exercise): RedirectResponse
{
$user = Auth::user();
$exists = $user->favoriteExercises()->where('exercise_id', $exercise->id)->exists();
if ($exists) {
$user->favoriteExercises()->detach($exercise->id);
$msg = 'Removed from favorites';
} else {
$user->favoriteExercises()->attach($exercise->id);
$msg = 'Added to favorites';
}
return back()->with('status', $msg);
}
public function toggleWorkout(Workout $workout): RedirectResponse
{
$user = Auth::user();
if ($workout->user_id && $workout->user_id !== $user->id) {
// Users can still favorite others' workouts if shared later; for now no restriction besides existence
}
$exists = $user->favoriteWorkouts()->where('workout_id', $workout->id)->exists();
if ($exists) {
$user->favoriteWorkouts()->detach($workout->id);
$msg = 'Removed from favorites';
} else {
$user->favoriteWorkouts()->attach($workout->id);
$msg = 'Added to favorites';
}
return back()->with('status', $msg);
}
public function toggleYogaPose(YogaPose $pose): RedirectResponse
{
$user = Auth::user();
$exists = $user->favoriteYogaPoses()->where('yoga_pose_id', $pose->id)->exists();
if ($exists) {
$user->favoriteYogaPoses()->detach($pose->id);
$msg = 'Removed from favorites';
} else {
$user->favoriteYogaPoses()->attach($pose->id);
$msg = 'Added to favorites';
}
return back()->with('status', $msg);
}
}