F
Filament16mo ago
N1XN

Access nested sum

How can I display the dose_count inside a TextColumn?
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with(['patients' => function ($query) {
$query->withSum('latestPatientProtocol', 'dose_count');
}]);
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with(['patients' => function ($query) {
$query->withSum('latestPatientProtocol', 'dose_count');
}]);
}
Solution:
I am not sure, because I solved this - but I think this way I would loose sorting, or would have to implement it manually. I fixed it by defining the needed relationship inside the model, so I don't have to traverse. https://github.com/staudenmeir/eloquent-has-many-deep...
GitHub
GitHub - staudenmeir/eloquent-has-many-deep: Laravel Eloquent HasMa...
Laravel Eloquent HasManyThrough relationships with unlimited levels - GitHub - staudenmeir/eloquent-has-many-deep: Laravel Eloquent HasManyThrough relationships with unlimited levels
Jump to solution
6 Replies
Dennis Koch
Dennis Koch16mo ago
Doesn't TextColumn::make('patients.dose_count') work?
N1XN
N1XNOP16mo ago
@Dennis Koch unfortunately not. Maybe some information are missing from my side. Hospital ->hasMany -> Doctor -> hasMany -> Patient -> hasMany PatientProtocol I want from the HospitalResource to sum all doses.
Dennis Koch
Dennis Koch16mo ago
If it's the HospitalResource, then you are missing the link to doctors? Not sure if that works with Eloquent. Maybe you need to write your own subselect.
N1XN
N1XNOP16mo ago
The Hospital has a relation to Patients through Doctors (HasManyThrough). So I actually can sum other columns that are in the patient table already. But going one more deep is kind of not working.
Dennis Koch
Dennis Koch16mo ago
I think the issue is, that you have an array of patients then. So you'd need to sum those too:
TextColumn::make('doses')
->getStateUsing(fn ($record) => $record->patients->sum('dose_count')
TextColumn::make('doses')
->getStateUsing(fn ($record) => $record->patients->sum('dose_count')
Solution
N1XN
N1XN16mo ago
I am not sure, because I solved this - but I think this way I would loose sorting, or would have to implement it manually. I fixed it by defining the needed relationship inside the model, so I don't have to traverse. https://github.com/staudenmeir/eloquent-has-many-deep
GitHub
GitHub - staudenmeir/eloquent-has-many-deep: Laravel Eloquent HasMa...
Laravel Eloquent HasManyThrough relationships with unlimited levels - GitHub - staudenmeir/eloquent-has-many-deep: Laravel Eloquent HasManyThrough relationships with unlimited levels
Want results from more Discord servers?
Add your server