Files
besttechfrom.eu/app/Http/Controllers/ProfileController.php
T

104 lines
3.1 KiB
PHP
Raw Normal View History

2026-02-06 14:33:11 +02:00
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
class ProfileController extends Controller
{
/**
* Display the specified user's profile.
*/
public function show(User $user)
{
2026-02-14 16:11:09 +02:00
// Get user's products
$products = $user->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'));
2026-02-06 14:33:11 +02:00
}
/**
* 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);
}
2026-02-06 14:33:11 +02:00
// 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!');
}
}