'required|string', 'email' => 'required|email', 'role' => 'required|string', 'avatar' => 'nullable|sometimes|image|max:1024', ]; protected $listeners = [ 'delete_user' => 'deleteUser', 'update_user' => 'updateUser', ]; public function render() { $roles = Role::all(); $roles_description = [ 'administrator' => 'Best for business owners and company administrators', 'developer' => 'Best for developers or people primarily using the API', 'analyst' => 'Best for people who need full access to analytics data, but don\'t need to update business settings', 'support' => 'Best for employees who regularly refund payments and respond to disputes', 'trial' => 'Best for people who need to preview content data, but don\'t need to make any updates', ]; foreach ($roles as $i => $role) { $roles[$i]->description = $roles_description[$role->name] ?? ''; } return view('livewire.user.add-user-modal', compact('roles')); } public function submit() { // Validate the form input data $this->validate(); DB::transaction(function () { // Prepare the data for creating a new user $data = [ 'name' => $this->name, ]; if ($this->avatar) { $data['profile_photo_path'] = $this->avatar->store('avatars', 'public'); } else { $data['profile_photo_path'] = null; } if (!$this->edit_mode) { $data['password'] = Hash::make($this->email); } // Create a new user record in the database $user = $this->user_id ? User::find($this->user_id) : User::updateOrCreate([ 'email' => $this->email, ], $data); if ($this->edit_mode) { foreach ($data as $k => $v) { $user->$k = $v; } } if ($this->edit_mode) { // Assign selected role for user $user->syncRoles($this->role); // Emit a success event with a message $this->emit('success', __('User updated')); } else { // Assign selected role for user $user->assignRole($this->role); // Send a password reset link to the user's email Password::sendResetLink($user->only('email')); // Emit a success event with a message $this->emit('success', __('New user created')); } }); // Reset the form fields after successful submission $this->reset(); } public function deleteUser($id) { // Prevent deletion of current user if ($id == Auth::id()) { $this->emit('error', 'User cannot be deleted'); return; } // Delete the user record with the specified ID User::destroy($id); // Emit a success event with a message $this->emit('success', 'User successfully deleted'); } public function updateUser($id) { $this->edit_mode = true; $user = User::find($id); $this->user_id = $user->id; $this->saved_avatar = $user->profile_photo_url; $this->name = $user->name; $this->email = $user->email; $this->role = $user->roles?->first()->name ?? ''; } public function hydrate() { $this->resetErrorBag(); $this->resetValidation(); } }