F
Filament15mo ago
ziolupo

Conditional widget based on RelationManager

I have a resource with two relationmanager: public static function getRelations(): array { return [ RelationManagers\ProposalRelationManager::class, RelationManagers\PartecipantsRelationManager::class, ]; } The problem is that I want to show different widgets, based on different table when I switch tab from one relation to the other. As showed above I have Proposal a Partecipant resource with different widgets Inside the edit page I have: protected function getHeaderWidgets(): array { return [ ProposalResource\Widgets\ProposalOverview::class, ]; } but of course it's ok when the I hit the "proposal Tab", but when I'm clicking the Partecipant tab, I would like to load another widget. Is this possible? Thank you!
1 Reply
pier
pier10mo ago
I've found a solution. I don't know if it's the best way, but it works. If you want to conditionally show widgets on the View page, you can access the public property activeRelationManager from the HasRelationManagers trait. Then, you can populate the widget array in the getHeaderWidgets function. In your case, it could be like this: protected function getHeaderWidgets(): array { $activeRelationManager = $this->activeRelationManager ?? "0"; $widgets = []; if($activeRelationManager === "0") { // first tab $widgets[] = ProposalResource\Widgets\ProposalOverview::class; } return $widgets; }
Want results from more Discord servers?
Add your server