subtract 2 column table

Hello, i have 2 column like this
TextColumn::make('quota')->label('Quota'),
TextColumn::make('participant_count')->counts('participant')->label('Terisi'),
TextColumn::make('quota')->label('Quota'),
TextColumn::make('participant_count')->counts('participant')->label('Terisi'),
i want to subtraction between Quota and Terisi, and make only 1 column. How can i achieve that in Filament? Thank you!
No description
Solution:
Now this is work, @charleswilfriedk thank youuu!!!
TextColumn::make('participants')
->formatStateUsing(fn(ClassChoice $record) => $record->quota-$record->participants->count()),
TextColumn::make('participants')
->formatStateUsing(fn(ClassChoice $record) => $record->quota-$record->participants->count()),
...
Jump to solution
16 Replies
charleswilfriedk
Hi, you can try this
TextColumn::make('quota')->formatStateUsing(
fn(Model $record) => $record->quota - $record->participant_count)),
TextColumn::make('quota')->formatStateUsing(
fn(Model $record) => $record->quota - $record->participant_count)),
Or append an attribute to your model like this
protected function attribute_name(): Attribute
{
return Attribute::make(
get: fn () => $this->quota - $this->participant_count,
}
protected function attribute_name(): Attribute
{
return Attribute::make(
get: fn () => $this->quota - $this->participant_count,
}
and use it like this
TextColumn::make('attribute_name')
TextColumn::make('attribute_name')
thyk123
thyk123OP9mo ago
Hello Charles! Thank you for being so concerned with my question. I appreciate that! I already tried the first but it shows nothing.
charleswilfriedk
Do you get a error message with the first solution ?
thyk123
thyk123OP9mo ago
@charleswilfriedk there is no error message in the first, but i try to modified and only show $this->participant_count, seems like it shows nothing
charleswilfriedk
Hum.. ok and the 2nd solution ? For the first solution i tried something similar and it should be working. Can you verify that this works correctly :
TextColumn::make('quota')->formatStateUsing(fn(Model $record) => $record->quota),
TextColumn::make('participant_count')->formatStateUsing(fn(Model $record) => $record->participant_count),
TextColumn::make('quota')->formatStateUsing(fn(Model $record) => $record->quota),
TextColumn::make('participant_count')->formatStateUsing(fn(Model $record) => $record->participant_count),
thyk123
thyk123OP9mo ago
quota column is working fine, the participant_count isn't working if using this, is shown up
TextColumn::make('participant_count')->counts('participant')->label('Terisi')
TextColumn::make('participant_count')->counts('participant')->label('Terisi')
charleswilfriedk
Ok i see where the problem is! I think that i should be 'participants' if the relationship is hasMany or belongsToMany. Try this :
TextColumn::make('participants')->formatStateUsing(fn(Model $record) => $record->participants->count()),
TextColumn::make('participants')->formatStateUsing(fn(Model $record) => $record->participants->count()),
thyk123
thyk123OP9mo ago
I tried this but still shows nothing @charleswilfriedk I'm sorry if this question takes your precious time
this is relationship in ClassChoice model
public function participant(): HasMany
{
return $this->hasMany(Participant::class,'idClassChoice','id');
}
this is relationship in ClassChoice model
public function participant(): HasMany
{
return $this->hasMany(Participant::class,'idClassChoice','id');
}
TextColumn::make('participants')->formatStateUsing(fn(ClassChoice $record) => $record->participants->count()),
TextColumn::make('participants')->formatStateUsing(fn(ClassChoice $record) => $record->participants->count()),
this is your code with ClassChoice model
charleswilfriedk
Don't worry about this! I rely on help from the discord from time to time too Ok first the relationship type is hasMany so change the name to 'participants'
thyk123
thyk123OP9mo ago
Thank you so much! I'm really appreciate for your help!
charleswilfriedk
TextColumn::make('participants')->formatStateUsing(fn(ClassChoice $record) => $record->participants->count()),
TextColumn::make('participants')->formatStateUsing(fn(ClassChoice $record) => $record->participants->count()),
Only works if the relation name is correct
thyk123
thyk123OP9mo ago
so the relation name must plural, participants?
charleswilfriedk
yes that's the best practice for hasMany and belongsToMany relationships the count() at the end only works if the return type of $record->participants is a collection
thyk123
thyk123OP9mo ago
You're right, thank you so much for your help!
Solution
thyk123
thyk1239mo ago
Now this is work, @charleswilfriedk thank youuu!!!
TextColumn::make('participants')
->formatStateUsing(fn(ClassChoice $record) => $record->quota-$record->participants->count()),
TextColumn::make('participants')
->formatStateUsing(fn(ClassChoice $record) => $record->quota-$record->participants->count()),
relation name must plural because using hasMany
charleswilfriedk
Ok good! Happy to help.
Want results from more Discord servers?
Add your server