F
Filamentβ€’2y ago
usmcgator

counts not working on relationship

I'm trying to add a column that shows a count of the related records. This code correctly displays JSON output of the related records, so I know it's getting them correctly, but counts() doesn't work. This gets results from the relationship:
TextColumn::make('goals'),
TextColumn::make('goals'),
This fails with: Call to a member function goals() on null
TextColumn::make('goals_count')->counts("goals"),
TextColumn::make('goals_count')->counts("goals"),
Here are the relationships:
public function mission()
{
return $this->belongsTo(Mission::class, 'mission_id', 'id');
}

public function goals()
{
return $this->mission->goals()->with('mission');
}
public function mission()
{
return $this->belongsTo(Mission::class, 'mission_id', 'id');
}

public function goals()
{
return $this->mission->goals()->with('mission');
}
Where am I going wrong?
4 Replies
Dan Harrin
Dan Harrinβ€’2y ago
i dont think its a real relationship if you have to go through ->mission maybe you need a hasManyThrough() ?
usmcgator
usmcgatorOPβ€’2y ago
I tried that, and it correctly displayed the count for one mission, but reported no records for the second, even though there are.
$this->hasManyThrough(Goal::class, Mission::class, 'id', 'mission_id');
$this->hasManyThrough(Goal::class, Mission::class, 'id', 'mission_id');
Dan Harrin
Dan Harrinβ€’2y ago
okay, well a real relationship is the way to go here you need to work out whats wrong with the definition
usmcgator
usmcgatorOPβ€’2y ago
it was a relationship issue, we worked things out πŸ™‚ Thanks
Want results from more Discord servers?
Add your server