Arbitrary values for table column

I've got a Resource component that maps to Auditions in my app. An Audition HasMany Dates which also HasMany Slots and those Slots have a user_id to indicate whether anyone has signed up for them. I want to add a column that displays the count of "sign ups" - slots with a non-null user_id. I've used ->counts for another column but I don't think the same syntax works for a 2nd order relation along with a condition. Starting from $this representing the Audition model, I can easily create the value I want to display but I don't know how to incorporate arbitrary code in the TableBuilder. Is that possible? Thank you!
Solution:
I'm not sure whether you're trying to display this new calculation next to Auditions, or next to Dates. But I would expect using dot-notation and a whereNotNull() query clause would be the direction to go...
Jump to solution
5 Replies
Solution
DrByte
DrByte12mo ago
I'm not sure whether you're trying to display this new calculation next to Auditions, or next to Dates. But I would expect using dot-notation and a whereNotNull() query clause would be the direction to go
DrByte
DrByte12mo ago
I did something like this where I needed to count a relationship. In this case 'judge' is a BelongsTo relationship on the Votes model, where votes is a relation on the Entry model. I'm counting an Entry's votes (relation) where the judge (relation) is not null:
Tables\Columns\TextColumn::make('judgevotes_count')
->getStateUsing(fn (Entry $record): string => $record->votes->whereNotNull('judge')->count())
Tables\Columns\TextColumn::make('judgevotes_count')
->getStateUsing(fn (Entry $record): string => $record->votes->whereNotNull('judge')->count())
gmgarrison
gmgarrisonOP12mo ago
That worked perfectly almost without any changes. Thank you. I guess the answer to my broader question is that I can use ->getStateUsing and pass it a resource and use Eloquent with it. Cool.
gmgarrison
gmgarrisonOP12mo ago
If you've got any advice, how should I understand "state" in that method name? Is it the same as the value of the cell in the table?
DrByte
DrByte12mo ago
Yes, $state is the value of the field
Want results from more Discord servers?
Add your server