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 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 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);
}
}
No description
Solution:
Can't you just do something like this to test? $this->get(Notes::getUrl([..params..]))?
Jump to solution
2 Replies
ikbentomas
ikbentomasOP3w ago
I've updated the post and maybe it's easier to grasp with this screenshot
Solution
Dennis Koch
Dennis Koch3w ago
Can't you just do something like this to test? $this->get(Notes::getUrl([..params..]))?

Did you find this page helpful?