setPrimaryKey('id') ->setDefaultSort('name', 'asc') ->setSortingPillsEnabled(false) ->setSecondaryHeaderEnabled() ->setAdditionalSelects(['features.id as id', 'features.layer_id as layer_id', 'features.feature_type_id as feature_type_id']); } #[On('features-changed')] public function refreshRows(): void { // } private function canManage(): bool { $user = Auth::user(); return $user->can('manage all') || ($user->can('edit layers') && Project::whereKey($this->projectId)->whereHas('users', fn ($q) => $q->where('user_id', $user->id))->exists()); } public function builder(): Builder { abort_unless($this->canManage(), 403); return Feature::query() ->whereHas('layer.phase', fn ($q) => $q->where('project_id', $this->projectId)) ->with(['layer.phase', 'featureType']); } public function columns(): array { return [ Column::make('Elemento', 'name') ->sortable()->searchable() ->secondaryHeaderFilter('name') ->format(fn ($value) => '' . e($value) . '') ->html(), Column::make('Capa') ->secondaryHeaderFilter('layer') ->label(fn ($row) => e($row->layer?->name ?? '—')), Column::make('Fase') ->label(fn ($row) => e($row->layer?->phase?->name ?? '—')), Column::make('Tipo') ->secondaryHeaderFilter('type') ->label(fn ($row) => $row->featureType ? '' . e($row->featureType->name) . '' : '') ->html(), Column::make('Activo', 'is_active') ->sortable() ->label(function ($row) { if ($row->is_active) { return ''; } return ''; }) ->html(), Column::make('Acciones') ->label(fn ($row) => '
') ->html(), ]; } public function filters(): array { $types = FeatureType::orderBy('name')->pluck('name', 'id')->toArray(); return [ TextFilter::make('Elemento', 'name') ->filter(fn (Builder $q, string $v) => $q->where('features.name', 'like', '%' . $v . '%')), SelectFilter::make('Capa', 'layer') ->options(['' => 'Todas'] + \App\Models\Layer::whereHas('phase', fn ($q) => $q->where('project_id', $this->projectId))->orderBy('name')->pluck('name', 'id')->toArray()) ->filter(fn (Builder $q, string $v) => $q->where('features.layer_id', $v)), SelectFilter::make('Tipo', 'type') ->options(['' => 'Todos'] + $types) ->filter(fn (Builder $q, string $v) => $q->where('features.feature_type_id', $v)), ]; } public function toggleActive(int $id): void { abort_unless($this->canManage(), 403); $feature = $this->findFeature($id); $feature->update(['is_active' => ! $feature->is_active]); $this->dispatch('features-changed'); $this->dispatch('notify', $feature->is_active ? 'Elemento activado' : 'Elemento desactivado'); } public function deleteFeature(int $id): void { abort_unless($this->canManage(), 403); $this->findFeature($id)->delete(); $this->dispatch('features-changed'); $this->dispatch('notify', 'Elemento eliminado'); } private function findFeature(int $id): Feature { return Feature::whereHas('layer.phase', fn ($q) => $q->where('project_id', $this->projectId))->findOrFail($id); } }