products() ->with(['category', 'user']) ->approved() ->withCount('upvotes') ->latest() ->paginate(12); // Get user's stats $stats = [ 'products_count' => $user->products()->approved()->count(), 'total_upvotes' => $user->products()->approved()->withCount('upvotes')->get()->sum('upvotes_count'), 'comments_count' => $user->comments()->count(), ]; return view('profile.show', compact('user', 'products', 'stats')); } /** * Show the form for editing the authenticated user's profile. */ public function edit() { $user = Auth::user(); return view('profile.edit', compact('user')); } /** * Update the authenticated user's profile. */ public function update(Request $request) { $user = Auth::user(); $validated = $request->validate([ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->ignore($user->id)], 'avatar' => ['nullable', 'image', 'mimes:jpeg,png,jpg,gif', 'max:2048'], 'bio' => ['nullable', 'string', 'max:1000'], 'website' => ['nullable', 'url', 'max:255'], 'twitter' => ['nullable', 'string', 'max:255'], 'github' => ['nullable', 'string', 'max:255'], 'linkedin' => ['nullable', 'string', 'max:255'], ]); // Handle avatar upload if ($request->hasFile('avatar')) { // Delete old avatar if exists if ($user->avatar) { Storage::disk('public')->delete('avatars/' . $user->avatar); } // Store new avatar $avatarName = time() . '_' . $user->id . '.' . $request->avatar->extension(); $request->avatar->storeAs('avatars', $avatarName, 'public'); $validated['avatar'] = $avatarName; } // Regenerate slug if name changed if ($request->name !== $user->name) { $validated['slug'] = User::generateUniqueSlug($request->name); } // Update user profile $user->update($validated); return redirect()->route('profile.show', $user) ->with('success', 'Profile updated successfully!'); } /** * Delete the user's avatar. */ public function deleteAvatar() { $user = Auth::user(); if ($user->avatar) { Storage::disk('public')->delete('avatars/' . $user->avatar); $user->update(['avatar' => null]); } return redirect()->route('profile.edit') ->with('success', 'Avatar deleted successfully!'); } }