F
Filament4mo ago
morty

Is there a more succinct way of retrieving a count of a resource inside a `ManageRelatedRecords`?

I'm trying to add a navigation badge to a record sub navigation item. Inside my ManageRelatedRecords page, I'm doing the following:
public static function getNavigationBadge(): ?string
{
return self::getResource()::getModel()::where(
'name_and_address_id',
request()->route()->parameter('record')
)
->firstOrFail()
->notes()
->count();
}
public static function getNavigationBadge(): ?string
{
return self::getResource()::getModel()::where(
'name_and_address_id',
request()->route()->parameter('record')
)
->firstOrFail()
->notes()
->count();
}
I'm wondering if there is a way to get the parent record without doing the request()->route()->parameter('record') search?
5 Replies
morty
mortyOP3mo ago
I was getting away with this for a while until I realized it doesn't work with table polling. I'm assuming because the way requests work in livewire and ajax not having the url parameter anymore. If anyone knows of a way to get the resource record let me know. Because the function is static, I can't use $this->getOwnerRecord()
David | Fortune Validator
did you ever get a solution for this? I am struggling to gain access to the parent record as well I went with this:
public static function getNavigationBadge(): ?string
{
$count = static::getResource()::getModel()::find(request()->route('record'))?->notes->count();
if($count > 0){
return $count;
}
return null;
}
public static function getNavigationBadge(): ?string
{
$count = static::getResource()::getModel()::find(request()->route('record'))?->notes->count();
if($count > 0){
return $count;
}
return null;
}
morty
mortyOP7d ago
I ran into so many issues with this I just abandoned it. I forget what they were now but it was briefly working then ran into other issues.
David | Fortune Validator
shame. Try that snippet above. I tried yours but the it was failing when I added a record. Try my code, seems okay for me
morty
mortyOP7d ago
it might have been with the polling because polling wont have access to the request()->route() enable polling on your table and see if it continues to work
Want results from more Discord servers?
Add your server