Reservasi-Cafe/app/Http/Controllers/UserProfileController.php

70 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use App\Models\User;
use Illuminate\Validation\Rules;
class UserProfileController extends Controller
{
public function show()
{
return view('user.profile');
}
public function updateProfile(Request $request)
{
$user = auth()->user();
$validated = $request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,' . $user->id],
'phone' => ['nullable', 'string', 'max:20'],
'profile_picture' => ['nullable', 'image', 'max:2048'], // Max 2MB
]);
if ($request->hasFile('profile_picture')) {
// Delete old profile picture if exists
if ($user->profile_picture) {
Storage::disk('public')->delete($user->profile_picture);
}
// Store new profile picture
$path = $request->file('profile_picture')->store('profile-pictures', 'public');
$validated['profile_picture'] = $path;
}
$user->update($validated);
return back()->with('profile_status', 'Profil berhasil diperbarui!');
}
public function updatePassword(Request $request)
{
$validated = $request->validate([
'current_password' => ['required', 'current_password'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
auth()->user()->update([
'password' => Hash::make($validated['password']),
]);
return back()->with('password_status', 'Password berhasil diperbarui!');
}
public function deleteProfilePicture()
{
$user = auth()->user();
if ($user->profile_picture) {
Storage::disk('public')->delete($user->profile_picture);
$user->update(['profile_picture' => null]);
}
return back()->with('profile_status', 'Foto profil berhasil dihapus!');
}
}