project = $project; $this->loadAvailable(); } /** Users not yet assigned to the project (for the dropdown). */ public function loadAvailable(): void { $assignedIds = $this->project->users()->pluck('users.id')->toArray(); $this->allUsers = User::whereNotIn('id', $assignedIds)->orderBy('name')->get(); } /** Reload the dropdown when the embedded table changes assignments. */ #[On('project-users-changed')] public function onUsersChanged(): void { $this->loadAvailable(); } public function assignUser() { abort_unless(Auth::user()->can('assign users'), 403); $this->validate([ 'selectedUserId' => 'required|exists:users,id', 'selectedRole' => 'required|in:' . implode(',', array_keys(ProjectUsersTable::ROLES)), ]); $this->project->users()->attach($this->selectedUserId, [ 'role_in_project' => $this->selectedRole, ]); $this->reset(['selectedUserId', 'selectedRole']); $this->loadAvailable(); $this->dispatch('project-users-changed'); $this->dispatch('notify', 'Usuario asignado al proyecto.'); } public function render() { return view('livewire.project-users', [ 'roles' => ProjectUsersTable::ROLES, ]); } }