re-render sidebar when item is selected

I'm probably doing this wrong by putting it in the boot method, but I have a dropdown in the topbar that sets a value in the session. Whenever that changes, or is cleared, I want the sidebar to refresh. I'm doing this in the boot menu, which is likely not right as it's only running once, but I tried adding a render method and didn't make a difference.
protected $selectedLocationId = false;
protected $selectedLocationName = false;

public function boot()
{
$selectedLocation = Session::get('selectedLocation');
if (!empty($selectedLocation)) {
$this->selectedLocationId = $selectedLocation->id;
$this->selectedLocationName = $selectedLocation->name;
}
}
protected $selectedLocationId = false;
protected $selectedLocationName = false;

public function boot()
{
$selectedLocation = Session::get('selectedLocation');
if (!empty($selectedLocation)) {
$this->selectedLocationId = $selectedLocation->id;
$this->selectedLocationName = $selectedLocation->name;
}
}
And then I'm trying to add this navigation that should change based on the selection:
->navigationGroups([
NavigationGroup::make()
->label(function () {
return $this->selectedLocationName;
})
->collapsed()
])
->navigationItems([
NavigationItem::make('details')
->label('Details')
->visible($this->selectedLocationId ?? false)
->url(function () {
if ($this->selectedLocationId) {
return LocationResource::getUrl('details', ['record' => $this->selectedLocationId]);
} else {
return false;
}
}),
->navigationGroups([
NavigationGroup::make()
->label(function () {
return $this->selectedLocationName;
})
->collapsed()
])
->navigationItems([
NavigationItem::make('details')
->label('Details')
->visible($this->selectedLocationId ?? false)
->url(function () {
if ($this->selectedLocationId) {
return LocationResource::getUrl('details', ['record' => $this->selectedLocationId]);
} else {
return false;
}
}),
Also, I have database notifications enabled, so I see the update in the network tab in dev tools happening every few seconds or so, but when I do a Log::debug() from the boot method, it puts log entries like multiple times a second. I don't know why the boot method would be getting called that frequently, but I have been noticing recently that my log file is too big to view.
3 Replies
Jon Mason
Jon Mason9mo ago
at what point in the lifecycle is the panel() method called? I moved it from the boot to the panel method thinking it might help, but even then it's not seeing a session value even when there is one. So is the session not available at the point in time when the panel method is called? is there a better way to accomplish what I'm after?
DrByte
DrByte9mo ago
Panels are service-providers, so ya they fire way before a lot of other services are established. The panel() method is called when the register() method is fired on all the ServiceProviders registered to Laravel. https://github.com/filamentphp/filament/blob/d227c098b95c0474e419295043fd43e610220a19/packages/panels/src/PanelProvider.php
GitHub
filament/packages/panels/src/PanelProvider.php at d227c098b95c0474e...
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS. - filamentphp/filament
binaryfire
binaryfire6mo ago
@Jon Mason Did you manage to figure this out? I'm using dynamically constructed navigation in one of my panels and need to make the sidebar live so it updates when I make changes to a particular resource