feature: update avatar

This commit is contained in:
antv
2026-07-09 09:05:57 +07:00
parent 439eec9015
commit 25bb3d961e
6 changed files with 129 additions and 10 deletions
+24 -9
View File
@@ -71,20 +71,35 @@ class UserController extends Controller
return view('user.change_password');
}
public function updatePassword(Request $request)
// Show edit profile page (avatar & password)
public function editProfile()
{
return view('user.edit');
}
// Handle avatar upload and optional password change
public function updateProfile(Request $request)
{
$request->validate([
'password' => 'required|min:6|confirmed',
], [
'password.required' => 'Mật khẩu mới không được để trống.',
'password.min' => 'Mật khẩu mới phải có ít nhất 6 ký tự.',
'password.confirmed' => 'Mật khẩu xác nhận không trùng khớp.',
'avatar' => 'nullable|image|max:2048', // 2MB max
'new_password' => 'nullable|min:6|confirmed',
]);
$user = Auth::user();
$this->userService->updatePassword($user, $request->password);
$route = $user->role == config('constants.ROLE_ADMIN') ? 'admin.dashboard' : 'user.dashboard';
return redirect()->route($route)->with('success', __('messages.password_change_success'));
// Avatar handling
if ($request->hasFile('avatar')) {
$path = $request->file('avatar')->store('avatars', 'public');
$user->avatar = 'storage/' . $path;
}
// Password handling (optional)
if ($request->filled('new_password')) {
$this->userService->updatePassword($user, $request->new_password);
}
$user->save();
return redirect()->route('user.edit')->with('success', 'Cập nhật hồ sơ thành công');
}
}
+22
View File
@@ -67,4 +67,26 @@ class User extends Authenticatable
{
return $this->hasMany(AddCard::class, 'buyer', 'msnv');
}
/**
* Get the user's avatar URL.
* If the user doesn't have an avatar, returns the default avatar.
*
* @param string|null $value
* @return string
*/
public function getAvatarAttribute($value)
{
if (!$value) {
return asset('images/avatar.png');
}
// Return direct URL if it's already an absolute URL (for backward compatibility)
if (filter_var($value, FILTER_VALIDATE_URL)) {
return $value;
}
return asset($value);
}
}