Infolist - belongsToMany collection.count not rendering?
Hi all<
I have 2 eloguent models with a many to many relations. I know the relationship is set up correctly in eloquent as a RepeatableEntry filed renders the individual members of the collection properly. I also want to output the count of the items in the collection with:
TextEntry::make('declines.count')
->Label("Quotes Declined")
->numeric(),
But I don't get anything displayed and no error messages. This works and displays the individual memebers.
RepeatableEntry::make('declines')
->schema([
TextEntry::make('name')
->bulleted()
->hiddenLabel(),
])->contained(false),
Any help appreciated.t
18 Replies
You can select the state directly:
->state(fn ($record) => $record->declines->count())
Or modify the DB query to return the counts and then access them via decline_counts
And I thought that one doesn't exist, because it's not mentioned in the docs 😅
I think this is new in v4 😅
Thanks. I am using v4 but couldn't get the ->count('declines') to work so when with the ->state option. The ->count is more elegant if I could get it to work.
php artisan filament:about
what is the output?And can you share the exact code you used?
Hi @LeandroFerreira
Here is the output:
Filament .........................................................................................................................................
Blade Icons ........................................................................................................................... NOT CACHED
Packages ................................................... filament, forms, notifications, support, tables, actions, infolists, schemas, widgets
Panel Components ...................................................................................................................... NOT CACHED
Version ................................................................................................................................... v4.0.3
Views .............................................................................................................................. NOT PUBLISHED
Blade Icons ........................................................................................................................... NOT CACHED
Packages ................................................... filament, forms, notifications, support, tables, actions, infolists, schemas, widgets
Panel Components ...................................................................................................................... NOT CACHED
Version ................................................................................................................................... v4.0.3
Views .............................................................................................................................. NOT PUBLISHED
Did you use
declines_count
as the name?counts, not count
GitHub
filament/packages/infolists/src/Components/TextEntry.php at v4.0.3 ...
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire - filamentphp/filament
Here is the code:
ComponentsSection::make('Details')
->description('Details')
->columns(3)->schema(
[
TextEntry::make('matching_vendors_count')
->Label("Invited")
->numeric(),
TextEntry::make('vendorDeclines_count')
->state(fn($record) => $record->vendorDeclines->count())
->Label("Declined")
->numeric(),
TextEntry::make('vendorOffers_count')
->state(fn($record) => $record->vendorOffers->count())
->Label("Tendered")
->numeric(),
RepeatableEntry::make('vendors')
->label("Invited")
->schema([
TextEntry::make('name')
->bulleted()
->hiddenLabel(),
])->contained(false),
RepeatableEntry::make('vendorDeclines')
->label("Declined")
->schema([
TextEntry::make('name')
->bulleted()
->hiddenLabel(),
])->contained(false),
RepeatableEntry::make('vendorOffers')
->label("Tendered")
->schema([
TextEntry::make('name')
->bulleted()
->hiddenLabel(),
])->contained(false)
]
),
And what part of this code is using my example? Did you see my example?
TextEntry::make('vendorOffers_count')
->state(fn($record) => $record->vendorOffers->count())
->Label("Tendered")
->numeric(),
The 2nd line doesn't work with ->counts()
I will investigate further.
TextEntry::make('vendor_offers_count')->counts('vendor_offers')
For the future: Please use markdown to format your code. That makes it more readable.
i tried to find the format button but can't. I have used it before. It seems the interface has changed. Will try and locate it.
It’s markdown, or triple backticks. You can find an example in the #✅┊rules