allRoles = Role::all()->toArray(); } public function updatedPerPage(int $value): void { $this->resetPage(); } public function updatedSearch(string $value): void { $this->resetPage(); } #[On('userSaved')] public function showIndex(): void { $this->mode = 'index'; $this->resetPage(); $this->resetForm(); } public function showForm(?int $id = null): void { $this->mode = 'form'; $this->editingId = $id; if ($id) { $user = User::with('roles')->findOrFail($id); $this->fullname = $user->fullname; $this->email = $user->email; $this->roles = $user->roles->pluck('name')->toArray(); } else { $this->resetForm(); } } protected function resetForm(): void { $this->fullname = ''; $this->email = ''; $this->password = ''; $this->password_confirmation = ''; $this->roles = []; $this->editingId = null; } public function delete(int $id): void { User::findOrFail($id)->delete(); session()->flash('message', 'User deleted.'); $this->resetPage(); } public function save(): void { $rules = [ 'fullname' => ['required', 'string', 'max:255'], 'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')->ignore($this->editingId)], 'roles' => ['nullable', 'array'], 'roles.*' => ['string', Rule::exists('roles', 'name')], ]; if (! $this->editingId) { $rules['password'] = ['required', 'string', 'min:6', 'confirmed']; } elseif ($this->password) { $rules['password'] = ['string', 'min:6', 'confirmed']; } $this->validate($rules); $user = $this->editingId ? User::findOrFail($this->editingId) : new User; $user->fullname = $this->fullname; $user->email = $this->email; if ($this->password) { $user->password = $this->password; } $user->save(); $user->syncRoles($this->roles); session()->flash('message', 'User saved.'); $this->dispatch('userSaved'); } public function render() { $users = User::with('roles') ->when( $this->search, fn($q) => $q ->where('fullname', 'like', "%{$this->search}%") ->orWhere('email', 'like', "%{$this->search}%") ) ->orderByDesc('id') ->paginate($this->perPage); return view('components.user.manager', [ 'users' => $users, 'allRoles' => collect($this->allRoles), 'title' => $this->mode === 'index' ? 'Users' : ($this->editingId ? 'Edit User' : 'Create User'), ]); } }