user profiles
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?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)
|
||||
{
|
||||
return view('profile.show', compact('user'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
// 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!');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user