User Profile
You have the option to edit the current logged in user’s profile information (name, email, phone, location, profile picture) and password. To access this page, just click the “Laravel Examples/User Profile” link in the left sidebar or add /laravel-examples/user-profile
in the URL.
The store used for users form can be found in app/Http/Controllers/ProfileController
You can find the functions for users form in resources/views/user-profile
folder.
Profile Controller
public function update(Request $request)
{
$request->validate([
'name' => 'required|min:3|max:255',
'email' => 'required|email|max:255|unique:users,email,' . Auth::id(),
'location' => 'max:255',
'phone' => 'max:255',
'profile_picture' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
], [
'name.required' => 'Name is required',
'email.required' => 'Email is required',
]);
$user = User::find(Auth::id());
$picture = $request->file('profile_picture');
if ($user->profile_picture && $picture && file_exists(storage_path('app/public/' . $user->profile_picture))) {
unlink(storage_path('app/public/' . $user->profile_picture));
}
$user->update([
'name' => $request->name,
'email' => $request->email,
'location' => $request->location,
'phone' => $request->phone,
'profile_picture' => $picture ? '/storage/' . $picture->store('profile', 'public') : $user->profile_picture,
]);
return back()->with('success', 'Profile updated successfully.');
}
public function updatePassword(Request $request)
{
$request->validate([
'current_password' => 'required',
'new_password' => 'required',
'new_password_confirmation' => 'required|same:new_password',
'new_password' => 'required|min:6',
], [
'current_password.required' => 'Current password is required',
'new_password.required' => 'New password is required',
'new_password.min' => 'New password must be at least 6 characters',
'new_password.confirmed' => 'New password confirmation does not match',
]);
$id = Auth::id();
$user = User::findOrFail($id);
if (!Hash::check($request->current_password, $user->password)) {
throw ValidationException::withMessages([
'current_password' => 'The provided current password is incorrect.',
]);
}
$user->update([
'password' => Hash::make($request->new_password),
]);
return back()->with('success-parola', 'Password changed successfully.');
}