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

82 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
class ProfileController extends Controller
{
public function show()
{
return view('profile.show', [
'user' => Auth::user()
]);
}
public function edit()
{
return view('profile.edit', [
'user' => Auth::user()
]);
}
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)],
'phone' => ['nullable', 'string', 'max:20'],
'address' => ['nullable', 'string', 'max:255'],
'birth_date' => ['nullable', 'date'],
'gender' => ['nullable', Rule::in(['male', 'female'])],
'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 redirect()->route('profile.show')->with('success', 'Profil berhasil diperbarui!');
}
public function updatePassword(Request $request)
{
$validated = $request->validate([
'current_password' => ['required', 'current_password'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
Auth::user()->update([
'password' => Hash::make($validated['password'])
]);
return back()->with('success', '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('success', 'Foto profil berhasil dihapus!');
}
}