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
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();
}