UpdateUserProfileInformation.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Actions\Fortify;
  3. use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Support\Facades\Validator;
  5. use Illuminate\Validation\Rule;
  6. use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
  7. class UpdateUserProfileInformation implements UpdatesUserProfileInformation
  8. {
  9. /**
  10. * Validate and update the given user's profile information.
  11. *
  12. * @param mixed $user
  13. * @param array $input
  14. * @return void
  15. */
  16. public function update($user, array $input)
  17. {
  18. Validator::make($input, [
  19. 'name' => ['required', 'string', 'max:255'],
  20. 'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
  21. 'photo' => ['nullable', 'image', 'max:1024'],
  22. ])->validateWithBag('updateProfileInformation');
  23. if (isset($input['photo'])) {
  24. $user->updateProfilePhoto($input['photo']);
  25. }
  26. if ($input['email'] !== $user->email &&
  27. $user instanceof MustVerifyEmail) {
  28. $this->updateVerifiedUser($user, $input);
  29. } else {
  30. $user->forceFill([
  31. 'name' => $input['name'],
  32. 'email' => $input['email'],
  33. ])->save();
  34. }
  35. }
  36. /**
  37. * Update the given verified user's profile information.
  38. *
  39. * @param mixed $user
  40. * @param array $input
  41. * @return void
  42. */
  43. protected function updateVerifiedUser($user, array $input)
  44. {
  45. $user->forceFill([
  46. 'name' => $input['name'],
  47. 'email' => $input['email'],
  48. 'email_verified_at' => null,
  49. ])->save();
  50. $user->sendEmailVerificationNotification();
  51. }
  52. }