Tell Filament to refresh navigation badges

I display my navigation badges using:
public static function getNavigationBadge(): ?string
{
return Cache::get('navigation.vetting_photos.label', '…');
}
public static function getNavigationBadge(): ?string
{
return Cache::get('navigation.vetting_photos.label', '…');
}
When someone vets a photo from my custom page, I decrement the count by 1:
$current = Cache::get('navigation.vetting_photos.label', 0);
if ($current > 0) {
Cache::decrement('navigation.vetting_photos.label');
}
$current = Cache::get('navigation.vetting_photos.label', 0);
if ($current > 0) {
Cache::decrement('navigation.vetting_photos.label');
}
How can I tell the front end navigation to refresh so my badge shows the correct number immediately after vetting? I attempted to refresh using a javascript event, but this was on suggestion of ChatGPT and it doesn't seem like refreshNavigation exists:
FilamentView::registerRenderHook(
PanelsRenderHook::BODY_END,
fn (): string => <<<'HTML'
<script>
document.addEventListener('alpine:init', () => {
window.addEventListener('refresh-navigation', () => {
const panel = window.filament?.app?.panels?.admin;

if (panel?.refreshNavigation) {
console.log('does not run');
panel.refreshNavigation();
}
});
});
</script>
HTML
);
FilamentView::registerRenderHook(
PanelsRenderHook::BODY_END,
fn (): string => <<<'HTML'
<script>
document.addEventListener('alpine:init', () => {
window.addEventListener('refresh-navigation', () => {
const panel = window.filament?.app?.panels?.admin;

if (panel?.refreshNavigation) {
console.log('does not run');
panel.refreshNavigation();
}
});
});
</script>
HTML
);
Solution:
Should be $wire.$dispatch('refresh-sidebar')
Jump to solution
3 Replies
Solution
Dennis Koch
Dennis Koch2w ago
Should be $wire.$dispatch('refresh-sidebar')
keiron
keironOP2w ago
@Dennis Koch Amazing! Thank you. I am using
$this->js('$wire.$dispatch("refresh-sidebar")');
$this->js('$wire.$dispatch("refresh-sidebar")');
in my custom page to trigger the refresh. Is this the suggested approach or should I be doing it differently?
Dennis Koch
Dennis Koch2w ago
You can just use $this->dispatch('refresh-sidebar') from the backend.

Did you find this page helpful?