FilamentF
Filament16mo ago
lacymj

Counting relationships

I have a simple relationship between an Entry and a Jockey.

class Entry extends Model
{
public function jockey(): BelongsTo
{
return $this->belongsTo(Jockey::class);
}
}

class Jockey extends Model
{
public function entries(): hasMany
{
return $this->hasMany(Entry::class);
}
}

I created filament resources for both models and am trying to get the relationship counts on the table list. It works great on the JockeyResource with this:

Tables\Columns\TextColumn::make('entries_count')
->label('Entry Count')
->counts('entries'),

BUT, when I try to get the same entry count on the EntryResource using what I "think" should work:

Tables\Columns\TextColumn::make('jockey.entries_count')
->label('Entry Count')
->counts('jockey.entries'),

I get this error

Call to undefined method App\Models\Entry::jockey.entries()

What's interesting is that if I take off the count options and just do this:

Tables\Columns\TextColumn::make('jockey.entries')
->label('Entry Count'),

I get the entire Entry record for all the correct entries

Any ideas how I can just get the count? I don't need the entire record returned.
Was this page helpful?