F
Filamentβ€’4mo 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.
8 Replies
toeknee
toekneeβ€’4mo ago
I believe you want jockey_entries_count You count the relationship on your current model. So say jocket_entries_count would have a jockeyEntries relationship on the model
lacymj
lacymjOPβ€’4mo ago
Thanks for the response. I tried adding a jockeyEntries relationship to the Entry model and referencing jockey_entries but I get the same error - Call to undefined method App\Models\Entry::jockey_entries() It feels like I'm dancing around the solution, but haven't quite found the right combo πŸ˜‰
krekas
krekasβ€’4mo ago
I don't get it. On the entry resource you are trying to show how many entries have that entry from its jockey? it will always be one, won't it?
lacymj
lacymjOPβ€’4mo ago
I'm trying to show how many total entries a jockey has (basically the same data I'm showing on the Jockey resource), I just need it on the Entry resource as well. I was able to brute force it, but it seems like using the existing relationships would be more logical.
krekas
krekasβ€’4mo ago
So... jockey_count doesn't work? but still, entrie belongs to one jockey. so it will always have a count of one. I don't understand what are you trying to count exactly
lacymj
lacymjOPβ€’4mo ago
I figured out a way to make this work. To answer, jockey_count works fine, but as you say, theres only one Jockey per Entry - I'm looking for a count of all the other Entries (total entry count) for that particular Jockey.
krekas
krekasβ€’4mo ago
Can you show your solution?
lacymj
lacymjOPβ€’4mo ago
sure - I added this relation to the Entry model... public function jockey_entries() { return $this->hasManyThrough(Entry::class, Jockey::class, 'id', 'jockey_id', 'jockey_id', 'id'); } Then added this to the table the table Tables\Columns\TextColumn::make('jockey_entries_count') ->label('Entries') ->counts('jockey_entries'), Basically, I created a relationship back through itself and messed with the id's to get it to work
Want results from more Discord servers?
Add your server