project = $project; $this->loadAvailable(); } /** Companies not yet assigned to the project (for the dropdown). */ public function loadAvailable(): void { $assignedIds = $this->project->companies()->pluck('companies.id')->toArray(); $this->allCompanies = Company::whereNotIn('id', $assignedIds)->orderBy('name')->get(); } /** Reload the dropdown when the embedded table changes assignments. */ #[On('project-companies-changed')] public function onCompaniesChanged(): void { $this->loadAvailable(); } public function assignCompany() { abort_unless(Auth::user()->can('assign companies'), 403); $this->validate([ 'selectedCompanyId' => 'required|exists:companies,id', 'selectedRole' => 'required|in:' . implode(',', array_keys(ProjectCompaniesTable::ROLES)), ]); $this->project->companies()->attach($this->selectedCompanyId, [ 'role_in_project' => $this->selectedRole, ]); $this->reset(['selectedCompanyId', 'selectedRole']); $this->loadAvailable(); $this->dispatch('project-companies-changed'); $this->dispatch('notify', 'Empresa asignada al proyecto.'); } public function render() { return view('livewire.project-companies', [ 'roles' => ProjectCompaniesTable::ROLES, ]); } }