'array', 'responsabile_ids' => 'array', ]; public function tenant(): BelongsTo { return $this->belongsTo(Tenant::class); } public function parent(): BelongsTo { return $this->belongsTo(Gruppo::class, 'parent_id'); } public function children(): HasMany { return $this->hasMany(Gruppo::class, 'parent_id'); } public function diocesi(): BelongsTo { return $this->belongsTo(Diocesi::class); } public function individui(): BelongsToMany { return $this->belongsToMany(Individuo::class, 'gruppo_individuo') ->withPivot('ruolo_ids', 'ruolo_nel_gruppo', 'data_adesione') ->withTimestamps(); } public function getResponsabiliIds(): array { if (empty($this->responsabile_ids)) { return []; } if (is_array($this->responsabile_ids)) { return $this->responsabile_ids; } return json_decode($this->responsabile_ids, true) ?? []; } public function getResponsabili(): \Illuminate\Database\Eloquent\Collection { $ids = $this->getResponsabiliIds(); if (empty($ids)) { return new \Illuminate\Database\Eloquent\Collection(); } return Individuo::whereIn('id', $ids)->get(); } public function eventi(): BelongsToMany { return $this->belongsToMany(Evento::class, 'eventi_gruppi')->withTimestamps(); } public function documenti(): HasMany { return $this->hasMany(Documento::class, 'visibilita_target_id') ->where('visibilita_target_type', self::class); } public function getAncestors(): array { $ancestors = []; $current = $this->parent; while ($current) { $ancestors[] = $current; $current = $current->parent; } return $ancestors; } public function getDescendants(): array { $descendants = []; foreach ($this->children as $child) { $descendants[] = $child; $descendants = array_merge($descendants, $child->getDescendants()); } return $descendants; } public function getFullPathAttribute(): string { $path = [$this->nome]; foreach (array_reverse($this->getAncestors()) as $ancestor) { $path[] = $ancestor->nome; } return implode(' > ', array_reverse($path)); } }