Skip to content

need email_verified_at set null first to using newEmail()?  #14

@YugoSamakuhaku

Description

@YugoSamakuhaku

I use livewire, when updating an email I use

   if ($this->email != $this->user->email) {
             $this->user->email_verified_at = null;
             $this->user->newEmail($this->user->email);
  }

if email_verified_at is not set to null, newEmail() will not create a new record in the pending_user_emails table

this is my edit file

class Edit extends Component {
    public User $user;

    public $roles          = [];
    public $listsForFields = [];

    public $password              = '';
    public $password_confirmation = '';

    public $email = '';

    public function mount(User $user) {
        $this->user  = $user;
        $this->email = $this->user->email;
        $this->roles = $this->user->roles()->pluck('id')->toArray();
        $this->initListsForFields();
    }

    public function render() {
        return view('livewire.user.edit');
    }

    public function submit() {
        $this->validate();
        $this->user->password = $this->password;

        if ($this->email != $this->user->email) {
            $this->user->email_verified_at = null;
            $this->user->newEmail($this->user->email);
        } else {
            $this->user->save();
        }
        $this->user->roles()->sync($this->roles);

        return redirect()->route('users.index');
    }

    protected function rules() {
        return [
            'user.name'             => ['required', 'string', 'max:255'],
            'user.email'            => ['required', 'email', 'max:255', 'unique:users,email,' . $this->user->id],
            'user.biography'        => ['nullable'],
            'roles'                 => ['required', 'array'],
            'roles.*.id'            => ['integer', 'exists:roles,id'],
            'password'              => ['string', 'min:8', 'confirmed'],
            'password_confirmation' => ['string', 'min:8'],
        ];
    }

    public function initListsForFields(): void {
        $this->listsForFields['roles'] = Role::pluck('name', 'id')->toArray();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions