Show navigation badge in sub navigation with count of relationship
I've made pages for a resource and listed those pages as sub navigation items. In this particular case I have 'notes' as child resource of the parent resource 'Server' My goal is to show the amount of related notes and an indicator of important notes in the sub navigation.
I've made a working version like this in the Notes Page.
class Notes extends Page{ public static function getNavigationBadge(): string { $record = request('record'); return Note::query()->where('server_id', $record->id)->count(); }}
class Notes extends Page{ public static function getNavigationBadge(): string { $record = request('record'); return Note::query()->where('server_id', $record->id)->count(); }}
But the main disadvantages of this approach are the tests, I'm unable to mock the request class. And the value of
$record
$record
is sometimes the Server model and on the View page the value becomes just the ID of the model. I would prefer a solution like this method on the parent resource, where
Page $page
Page $page
is available.
class ServerResource extends Resource{ public static function getRecordSubNavigation(Page $page): array { $items = array_filter([ // doing some logic with the $page->record ]); return $page->generateNavigationItems($items); }}
class ServerResource extends Resource{ public static function getRecordSubNavigation(Page $page): array { $items = array_filter([ // doing some logic with the $page->record ]); return $page->generateNavigationItems($items); }}